obs.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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_gs_init_data(struct gs_init_data *gid,
  23. const struct obs_video_info *ovi)
  24. {
  25. memcpy(&gid->window, &ovi->window, sizeof(struct gs_window));
  26. gid->cx = ovi->window_width;
  27. gid->cy = ovi->window_height;
  28. gid->num_backbuffers = 2;
  29. gid->format = GS_RGBA;
  30. gid->zsformat = GS_ZS_NONE;
  31. gid->adapter = ovi->adapter;
  32. }
  33. static inline void make_video_info(struct video_output_info *vi,
  34. struct obs_video_info *ovi)
  35. {
  36. vi->name = "video";
  37. vi->format = ovi->output_format;
  38. vi->fps_num = ovi->fps_num;
  39. vi->fps_den = ovi->fps_den;
  40. vi->width = ovi->output_width;
  41. vi->height = ovi->output_height;
  42. vi->range = ovi->range;
  43. vi->colorspace = ovi->colorspace;
  44. vi->cache_size = 6;
  45. }
  46. #define PIXEL_SIZE 4
  47. #define GET_ALIGN(val, align) \
  48. (((val) + (align-1)) & ~(align-1))
  49. static inline void set_420p_sizes(const struct obs_video_info *ovi)
  50. {
  51. struct obs_core_video *video = &obs->video;
  52. uint32_t chroma_pixels;
  53. uint32_t total_bytes;
  54. chroma_pixels = (ovi->output_width * ovi->output_height / 4);
  55. chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);
  56. video->plane_offsets[0] = 0;
  57. video->plane_offsets[1] = ovi->output_width * ovi->output_height;
  58. video->plane_offsets[2] = video->plane_offsets[1] + chroma_pixels;
  59. video->plane_linewidth[0] = ovi->output_width;
  60. video->plane_linewidth[1] = ovi->output_width/2;
  61. video->plane_linewidth[2] = ovi->output_width/2;
  62. video->plane_sizes[0] = video->plane_offsets[1];
  63. video->plane_sizes[1] = video->plane_sizes[0]/4;
  64. video->plane_sizes[2] = video->plane_sizes[1];
  65. total_bytes = video->plane_offsets[2] + chroma_pixels;
  66. video->conversion_height =
  67. (total_bytes/PIXEL_SIZE + ovi->output_width-1) /
  68. ovi->output_width;
  69. video->conversion_height = GET_ALIGN(video->conversion_height, 2);
  70. video->conversion_tech = "Planar420";
  71. }
  72. static inline void set_nv12_sizes(const struct obs_video_info *ovi)
  73. {
  74. struct obs_core_video *video = &obs->video;
  75. uint32_t chroma_pixels;
  76. uint32_t total_bytes;
  77. chroma_pixels = (ovi->output_width * ovi->output_height / 2);
  78. chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);
  79. video->plane_offsets[0] = 0;
  80. video->plane_offsets[1] = ovi->output_width * ovi->output_height;
  81. video->plane_linewidth[0] = ovi->output_width;
  82. video->plane_linewidth[1] = ovi->output_width;
  83. video->plane_sizes[0] = video->plane_offsets[1];
  84. video->plane_sizes[1] = video->plane_sizes[0]/2;
  85. total_bytes = video->plane_offsets[1] + chroma_pixels;
  86. video->conversion_height =
  87. (total_bytes/PIXEL_SIZE + ovi->output_width-1) /
  88. ovi->output_width;
  89. video->conversion_height = GET_ALIGN(video->conversion_height, 2);
  90. video->conversion_tech = "NV12";
  91. }
  92. static inline void calc_gpu_conversion_sizes(const struct obs_video_info *ovi)
  93. {
  94. obs->video.conversion_height = 0;
  95. memset(obs->video.plane_offsets, 0, sizeof(obs->video.plane_offsets));
  96. memset(obs->video.plane_sizes, 0, sizeof(obs->video.plane_sizes));
  97. memset(obs->video.plane_linewidth, 0,
  98. sizeof(obs->video.plane_linewidth));
  99. switch ((uint32_t)ovi->output_format) {
  100. case VIDEO_FORMAT_I420:
  101. set_420p_sizes(ovi);
  102. break;
  103. case VIDEO_FORMAT_NV12:
  104. set_nv12_sizes(ovi);
  105. break;
  106. }
  107. }
  108. static bool obs_init_gpu_conversion(struct obs_video_info *ovi)
  109. {
  110. struct obs_core_video *video = &obs->video;
  111. calc_gpu_conversion_sizes(ovi);
  112. if (!video->conversion_height) {
  113. blog(LOG_INFO, "GPU conversion not available for format: %u",
  114. (unsigned int)ovi->output_format);
  115. video->gpu_conversion = false;
  116. return true;
  117. }
  118. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  119. video->convert_textures[i] = gs_texture_create(
  120. ovi->output_width, video->conversion_height,
  121. GS_RGBA, 1, NULL, GS_RENDER_TARGET);
  122. if (!video->convert_textures[i])
  123. return false;
  124. }
  125. return true;
  126. }
  127. static bool obs_init_textures(struct obs_video_info *ovi)
  128. {
  129. struct obs_core_video *video = &obs->video;
  130. uint32_t output_height = video->gpu_conversion ?
  131. video->conversion_height : ovi->output_height;
  132. size_t i;
  133. for (i = 0; i < NUM_TEXTURES; i++) {
  134. video->copy_surfaces[i] = gs_stagesurface_create(
  135. ovi->output_width, output_height, GS_RGBA);
  136. if (!video->copy_surfaces[i])
  137. return false;
  138. video->render_textures[i] = gs_texture_create(
  139. ovi->base_width, ovi->base_height,
  140. GS_RGBA, 1, NULL, GS_RENDER_TARGET);
  141. if (!video->render_textures[i])
  142. return false;
  143. video->output_textures[i] = gs_texture_create(
  144. ovi->output_width, ovi->output_height,
  145. GS_RGBA, 1, NULL, GS_RENDER_TARGET);
  146. if (!video->output_textures[i])
  147. return false;
  148. }
  149. return true;
  150. }
  151. static int obs_init_graphics(struct obs_video_info *ovi)
  152. {
  153. struct obs_core_video *video = &obs->video;
  154. struct gs_init_data graphics_data;
  155. bool success = true;
  156. int errorcode;
  157. make_gs_init_data(&graphics_data, ovi);
  158. errorcode = gs_create(&video->graphics, ovi->graphics_module,
  159. &graphics_data);
  160. if (errorcode != GS_SUCCESS) {
  161. switch (errorcode) {
  162. case GS_ERROR_MODULE_NOT_FOUND:
  163. return OBS_VIDEO_MODULE_NOT_FOUND;
  164. case GS_ERROR_NOT_SUPPORTED:
  165. return OBS_VIDEO_NOT_SUPPORTED;
  166. default:
  167. return OBS_VIDEO_FAIL;
  168. }
  169. }
  170. gs_enter_context(video->graphics);
  171. char *filename = find_libobs_data_file("default.effect");
  172. video->default_effect = gs_effect_create_from_file(filename,
  173. NULL);
  174. bfree(filename);
  175. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  176. filename = find_libobs_data_file("default_rect.effect");
  177. video->default_rect_effect = gs_effect_create_from_file(
  178. filename, NULL);
  179. bfree(filename);
  180. }
  181. filename = find_libobs_data_file("opaque.effect");
  182. video->opaque_effect = gs_effect_create_from_file(filename,
  183. NULL);
  184. bfree(filename);
  185. filename = find_libobs_data_file("solid.effect");
  186. video->solid_effect = gs_effect_create_from_file(filename,
  187. NULL);
  188. bfree(filename);
  189. filename = find_libobs_data_file("format_conversion.effect");
  190. video->conversion_effect = gs_effect_create_from_file(filename,
  191. NULL);
  192. bfree(filename);
  193. filename = find_libobs_data_file("bicubic_scale.effect");
  194. video->bicubic_effect = gs_effect_create_from_file(filename,
  195. NULL);
  196. bfree(filename);
  197. filename = find_libobs_data_file("lanczos_scale.effect");
  198. video->lanczos_effect = gs_effect_create_from_file(filename,
  199. NULL);
  200. bfree(filename);
  201. if (!video->default_effect)
  202. success = false;
  203. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  204. if (!video->default_rect_effect)
  205. success = false;
  206. }
  207. if (!video->opaque_effect)
  208. success = false;
  209. if (!video->solid_effect)
  210. success = false;
  211. if (!video->conversion_effect)
  212. success = false;
  213. gs_leave_context();
  214. return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
  215. }
  216. static inline void set_video_matrix(struct obs_core_video *video,
  217. struct obs_video_info *ovi)
  218. {
  219. struct matrix4 mat;
  220. struct vec4 r_row;
  221. if (format_is_yuv(ovi->output_format)) {
  222. video_format_get_parameters(ovi->colorspace, ovi->range,
  223. (float*)&mat, NULL, NULL);
  224. matrix4_inv(&mat, &mat);
  225. /* swap R and G */
  226. r_row = mat.x;
  227. mat.x = mat.y;
  228. mat.y = r_row;
  229. } else {
  230. matrix4_identity(&mat);
  231. }
  232. memcpy(video->color_matrix, &mat, sizeof(float) * 16);
  233. }
  234. static int obs_init_video(struct obs_video_info *ovi)
  235. {
  236. struct obs_core_video *video = &obs->video;
  237. struct video_output_info vi;
  238. int errorcode;
  239. make_video_info(&vi, ovi);
  240. video->base_width = ovi->base_width;
  241. video->base_height = ovi->base_height;
  242. video->output_width = ovi->output_width;
  243. video->output_height = ovi->output_height;
  244. video->gpu_conversion = ovi->gpu_conversion;
  245. video->scale_type = ovi->scale_type;
  246. set_video_matrix(video, ovi);
  247. errorcode = video_output_open(&video->video, &vi);
  248. if (errorcode != VIDEO_OUTPUT_SUCCESS) {
  249. if (errorcode == VIDEO_OUTPUT_INVALIDPARAM) {
  250. blog(LOG_ERROR, "Invalid video parameters specified");
  251. return OBS_VIDEO_INVALID_PARAM;
  252. } else {
  253. blog(LOG_ERROR, "Could not open video output");
  254. }
  255. return OBS_VIDEO_FAIL;
  256. }
  257. if (!obs_display_init(&video->main_display, NULL))
  258. return OBS_VIDEO_FAIL;
  259. video->main_display.cx = ovi->window_width;
  260. video->main_display.cy = ovi->window_height;
  261. gs_enter_context(video->graphics);
  262. if (ovi->gpu_conversion && !obs_init_gpu_conversion(ovi))
  263. return OBS_VIDEO_FAIL;
  264. if (!obs_init_textures(ovi))
  265. return OBS_VIDEO_FAIL;
  266. gs_leave_context();
  267. errorcode = pthread_create(&video->video_thread, NULL,
  268. obs_video_thread, obs);
  269. if (errorcode != 0)
  270. return OBS_VIDEO_FAIL;
  271. video->thread_initialized = true;
  272. return OBS_VIDEO_SUCCESS;
  273. }
  274. static void stop_video(void)
  275. {
  276. struct obs_core_video *video = &obs->video;
  277. void *thread_retval;
  278. if (video->video) {
  279. video_output_stop(video->video);
  280. if (video->thread_initialized) {
  281. pthread_join(video->video_thread, &thread_retval);
  282. video->thread_initialized = false;
  283. }
  284. }
  285. }
  286. static void obs_free_video(void)
  287. {
  288. struct obs_core_video *video = &obs->video;
  289. if (video->video) {
  290. obs_display_free(&video->main_display);
  291. video_output_close(video->video);
  292. video->video = NULL;
  293. if (!video->graphics)
  294. return;
  295. gs_enter_context(video->graphics);
  296. if (video->mapped_surface) {
  297. gs_stagesurface_unmap(video->mapped_surface);
  298. video->mapped_surface = NULL;
  299. }
  300. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  301. gs_stagesurface_destroy(video->copy_surfaces[i]);
  302. gs_texture_destroy(video->render_textures[i]);
  303. gs_texture_destroy(video->convert_textures[i]);
  304. gs_texture_destroy(video->output_textures[i]);
  305. video->copy_surfaces[i] = NULL;
  306. video->render_textures[i] = NULL;
  307. video->convert_textures[i] = NULL;
  308. video->output_textures[i] = NULL;
  309. }
  310. gs_leave_context();
  311. circlebuf_free(&video->vframe_info_buffer);
  312. memset(&video->textures_rendered, 0,
  313. sizeof(video->textures_rendered));
  314. memset(&video->textures_output, 0,
  315. sizeof(video->textures_output));
  316. memset(&video->textures_copied, 0,
  317. sizeof(video->textures_copied));
  318. memset(&video->textures_converted, 0,
  319. sizeof(video->textures_converted));
  320. video->cur_texture = 0;
  321. }
  322. }
  323. static void obs_free_graphics(void)
  324. {
  325. struct obs_core_video *video = &obs->video;
  326. if (video->graphics) {
  327. gs_enter_context(video->graphics);
  328. gs_effect_destroy(video->default_effect);
  329. gs_effect_destroy(video->default_rect_effect);
  330. gs_effect_destroy(video->opaque_effect);
  331. gs_effect_destroy(video->solid_effect);
  332. gs_effect_destroy(video->conversion_effect);
  333. gs_effect_destroy(video->bicubic_effect);
  334. gs_effect_destroy(video->lanczos_effect);
  335. video->default_effect = NULL;
  336. gs_leave_context();
  337. gs_destroy(video->graphics);
  338. video->graphics = NULL;
  339. }
  340. }
  341. static bool obs_init_audio(struct audio_output_info *ai)
  342. {
  343. struct obs_core_audio *audio = &obs->audio;
  344. int errorcode;
  345. /* TODO: sound subsystem */
  346. audio->user_volume = 1.0f;
  347. audio->present_volume = 1.0f;
  348. errorcode = audio_output_open(&audio->audio, ai);
  349. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  350. return true;
  351. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  352. blog(LOG_ERROR, "Invalid audio parameters specified");
  353. else
  354. blog(LOG_ERROR, "Could not open audio output");
  355. return false;
  356. }
  357. static void obs_free_audio(void)
  358. {
  359. struct obs_core_audio *audio = &obs->audio;
  360. if (audio->audio)
  361. audio_output_close(audio->audio);
  362. memset(audio, 0, sizeof(struct obs_core_audio));
  363. }
  364. static bool obs_init_data(void)
  365. {
  366. struct obs_core_data *data = &obs->data;
  367. pthread_mutexattr_t attr;
  368. assert(data != NULL);
  369. pthread_mutex_init_value(&obs->data.displays_mutex);
  370. if (pthread_mutexattr_init(&attr) != 0)
  371. return false;
  372. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  373. goto fail;
  374. if (pthread_mutex_init(&data->user_sources_mutex, &attr) != 0)
  375. goto fail;
  376. if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
  377. goto fail;
  378. if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
  379. goto fail;
  380. if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
  381. goto fail;
  382. if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
  383. goto fail;
  384. if (pthread_mutex_init(&data->services_mutex, &attr) != 0)
  385. goto fail;
  386. if (!obs_view_init(&data->main_view))
  387. goto fail;
  388. data->valid = true;
  389. fail:
  390. pthread_mutexattr_destroy(&attr);
  391. return data->valid;
  392. }
  393. void obs_main_view_free(struct obs_view *view)
  394. {
  395. if (!view) return;
  396. for (size_t i = 0; i < MAX_CHANNELS; i++)
  397. obs_source_release(view->channels[i]);
  398. memset(view->channels, 0, sizeof(view->channels));
  399. pthread_mutex_destroy(&view->channels_mutex);
  400. }
  401. #define FREE_OBS_LINKED_LIST(type) \
  402. do { \
  403. int unfreed = 0; \
  404. while (data->first_ ## type ) { \
  405. obs_ ## type ## _destroy(data->first_ ## type ); \
  406. unfreed++; \
  407. } \
  408. if (unfreed) \
  409. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  410. unfreed); \
  411. } while (false)
  412. static void obs_free_data(void)
  413. {
  414. struct obs_core_data *data = &obs->data;
  415. data->valid = false;
  416. obs_main_view_free(&data->main_view);
  417. blog(LOG_INFO, "Freeing OBS context data");
  418. if (data->user_sources.num)
  419. blog(LOG_INFO, "\t%d user source(s) were remaining",
  420. (int)data->user_sources.num);
  421. while (data->user_sources.num)
  422. obs_source_remove(data->user_sources.array[0]);
  423. da_free(data->user_sources);
  424. FREE_OBS_LINKED_LIST(source);
  425. FREE_OBS_LINKED_LIST(output);
  426. FREE_OBS_LINKED_LIST(encoder);
  427. FREE_OBS_LINKED_LIST(display);
  428. FREE_OBS_LINKED_LIST(service);
  429. pthread_mutex_destroy(&data->user_sources_mutex);
  430. pthread_mutex_destroy(&data->sources_mutex);
  431. pthread_mutex_destroy(&data->displays_mutex);
  432. pthread_mutex_destroy(&data->outputs_mutex);
  433. pthread_mutex_destroy(&data->encoders_mutex);
  434. pthread_mutex_destroy(&data->services_mutex);
  435. }
  436. static const char *obs_signals[] = {
  437. "void source_create(ptr source)",
  438. "void source_destroy(ptr source)",
  439. "void source_add(ptr source)",
  440. "void source_remove(ptr source)",
  441. "void source_activate(ptr source)",
  442. "void source_deactivate(ptr source)",
  443. "void source_show(ptr source)",
  444. "void source_hide(ptr source)",
  445. "void source_rename(ptr source, string new_name, string prev_name)",
  446. "void source_volume(ptr source, in out float volume)",
  447. "void source_volume_level(ptr source, float level, float magnitude, "
  448. "float peak)",
  449. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  450. "void master_volume(in out float volume)",
  451. NULL
  452. };
  453. static inline bool obs_init_handlers(void)
  454. {
  455. obs->signals = signal_handler_create();
  456. if (!obs->signals)
  457. return false;
  458. obs->procs = proc_handler_create();
  459. if (!obs->procs)
  460. return false;
  461. return signal_handler_add_array(obs->signals, obs_signals);
  462. }
  463. extern const struct obs_source_info scene_info;
  464. extern void log_system_info(void);
  465. static bool obs_init(const char *locale)
  466. {
  467. obs = bzalloc(sizeof(struct obs_core));
  468. log_system_info();
  469. if (!obs_init_data())
  470. return false;
  471. if (!obs_init_handlers())
  472. return false;
  473. obs->locale = bstrdup(locale);
  474. obs_register_source(&scene_info);
  475. add_default_module_paths();
  476. return true;
  477. }
  478. #ifdef _WIN32
  479. extern void initialize_crash_handler(void);
  480. #endif
  481. bool obs_startup(const char *locale)
  482. {
  483. bool success;
  484. if (obs) {
  485. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  486. return false;
  487. }
  488. #ifdef _WIN32
  489. initialize_crash_handler();
  490. #endif
  491. success = obs_init(locale);
  492. if (!success)
  493. obs_shutdown();
  494. return success;
  495. }
  496. void obs_shutdown(void)
  497. {
  498. struct obs_module *module;
  499. if (!obs)
  500. return;
  501. da_free(obs->input_types);
  502. da_free(obs->filter_types);
  503. da_free(obs->encoder_types);
  504. da_free(obs->transition_types);
  505. da_free(obs->output_types);
  506. da_free(obs->service_types);
  507. da_free(obs->modal_ui_callbacks);
  508. da_free(obs->modeless_ui_callbacks);
  509. stop_video();
  510. obs_free_data();
  511. obs_free_video();
  512. obs_free_graphics();
  513. obs_free_audio();
  514. proc_handler_destroy(obs->procs);
  515. signal_handler_destroy(obs->signals);
  516. module = obs->first_module;
  517. while (module) {
  518. struct obs_module *next = module->next;
  519. free_module(module);
  520. module = next;
  521. }
  522. obs->first_module = NULL;
  523. for (size_t i = 0; i < obs->module_paths.num; i++)
  524. free_module_path(obs->module_paths.array+i);
  525. da_free(obs->module_paths);
  526. bfree(obs->locale);
  527. bfree(obs);
  528. obs = NULL;
  529. }
  530. bool obs_initialized(void)
  531. {
  532. return obs != NULL;
  533. }
  534. uint32_t obs_get_version(void)
  535. {
  536. return LIBOBS_API_VER;
  537. }
  538. void obs_set_locale(const char *locale)
  539. {
  540. struct obs_module *module;
  541. if (!obs)
  542. return;
  543. if (obs->locale)
  544. bfree(obs->locale);
  545. obs->locale = bstrdup(locale);
  546. module = obs->first_module;
  547. while (module) {
  548. if (module->set_locale)
  549. module->set_locale(locale);
  550. module = module->next;
  551. }
  552. }
  553. const char *obs_get_locale(void)
  554. {
  555. return obs ? obs->locale : NULL;
  556. }
  557. #define OBS_SIZE_MIN 2
  558. #define OBS_SIZE_MAX (32 * 1024)
  559. static inline bool size_valid(uint32_t width, uint32_t height)
  560. {
  561. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  562. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  563. }
  564. int obs_reset_video(struct obs_video_info *ovi)
  565. {
  566. if (!obs) return OBS_VIDEO_FAIL;
  567. /* don't allow changing of video settings if active. */
  568. if (obs->video.video && video_output_active(obs->video.video))
  569. return OBS_VIDEO_CURRENTLY_ACTIVE;
  570. if (!size_valid(ovi->output_width, ovi->output_height) ||
  571. !size_valid(ovi->base_width, ovi->base_height))
  572. return OBS_VIDEO_INVALID_PARAM;
  573. struct obs_core_video *video = &obs->video;
  574. stop_video();
  575. obs_free_video();
  576. if (!ovi) {
  577. obs_free_graphics();
  578. return OBS_VIDEO_SUCCESS;
  579. }
  580. /* align to multiple-of-two and SSE alignment sizes */
  581. ovi->output_width &= 0xFFFFFFFC;
  582. ovi->output_height &= 0xFFFFFFFE;
  583. if (!video->graphics) {
  584. int errorcode = obs_init_graphics(ovi);
  585. if (errorcode != OBS_VIDEO_SUCCESS) {
  586. obs_free_graphics();
  587. return errorcode;
  588. }
  589. }
  590. blog(LOG_INFO, "video settings reset:\n"
  591. "\tbase resolution: %dx%d\n"
  592. "\toutput resolution: %dx%d\n"
  593. "\tfps: %d/%d",
  594. ovi->base_width, ovi->base_height,
  595. ovi->output_width, ovi->output_height,
  596. ovi->fps_num, ovi->fps_den);
  597. return obs_init_video(ovi);
  598. }
  599. bool obs_reset_audio(const struct obs_audio_info *oai)
  600. {
  601. struct audio_output_info ai;
  602. if (!obs) return false;
  603. /* don't allow changing of audio settings if active. */
  604. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  605. return false;
  606. obs_free_audio();
  607. if (!oai)
  608. return true;
  609. ai.name = "Audio";
  610. ai.samples_per_sec = oai->samples_per_sec;
  611. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  612. ai.speakers = oai->speakers;
  613. ai.buffer_ms = oai->buffer_ms;
  614. blog(LOG_INFO, "audio settings reset:\n"
  615. "\tsamples per sec: %d\n"
  616. "\tspeakers: %d\n"
  617. "\tbuffering (ms): %d\n",
  618. (int)ai.samples_per_sec,
  619. (int)ai.speakers,
  620. (int)ai.buffer_ms);
  621. return obs_init_audio(&ai);
  622. }
  623. bool obs_get_video_info(struct obs_video_info *ovi)
  624. {
  625. struct obs_core_video *video = &obs->video;
  626. const struct video_output_info *info;
  627. if (!obs || !video->graphics)
  628. return false;
  629. info = video_output_get_info(video->video);
  630. if (!info)
  631. return false;
  632. memset(ovi, 0, sizeof(struct obs_video_info));
  633. ovi->base_width = video->base_width;
  634. ovi->base_height = video->base_height;
  635. ovi->gpu_conversion= video->gpu_conversion;
  636. ovi->scale_type = video->scale_type;
  637. ovi->colorspace = info->colorspace;
  638. ovi->range = info->range;
  639. ovi->output_width = info->width;
  640. ovi->output_height = info->height;
  641. ovi->output_format = info->format;
  642. ovi->fps_num = info->fps_num;
  643. ovi->fps_den = info->fps_den;
  644. return true;
  645. }
  646. bool obs_get_audio_info(struct obs_audio_info *oai)
  647. {
  648. struct obs_core_audio *audio = &obs->audio;
  649. const struct audio_output_info *info;
  650. if (!obs || !oai || !audio->audio)
  651. return false;
  652. info = audio_output_get_info(audio->audio);
  653. oai->samples_per_sec = info->samples_per_sec;
  654. oai->speakers = info->speakers;
  655. oai->buffer_ms = info->buffer_ms;
  656. return true;
  657. }
  658. bool obs_enum_input_types(size_t idx, const char **id)
  659. {
  660. if (!obs) return false;
  661. if (idx >= obs->input_types.num)
  662. return false;
  663. *id = obs->input_types.array[idx].id;
  664. return true;
  665. }
  666. bool obs_enum_filter_types(size_t idx, const char **id)
  667. {
  668. if (!obs) return false;
  669. if (idx >= obs->filter_types.num)
  670. return false;
  671. *id = obs->filter_types.array[idx].id;
  672. return true;
  673. }
  674. bool obs_enum_transition_types(size_t idx, const char **id)
  675. {
  676. if (!obs) return false;
  677. if (idx >= obs->transition_types.num)
  678. return false;
  679. *id = obs->transition_types.array[idx].id;
  680. return true;
  681. }
  682. bool obs_enum_output_types(size_t idx, const char **id)
  683. {
  684. if (!obs) return false;
  685. if (idx >= obs->output_types.num)
  686. return false;
  687. *id = obs->output_types.array[idx].id;
  688. return true;
  689. }
  690. bool obs_enum_encoder_types(size_t idx, const char **id)
  691. {
  692. if (!obs) return false;
  693. if (idx >= obs->encoder_types.num)
  694. return false;
  695. *id = obs->encoder_types.array[idx].id;
  696. return true;
  697. }
  698. bool obs_enum_service_types(size_t idx, const char **id)
  699. {
  700. if (!obs) return false;
  701. if (idx >= obs->service_types.num)
  702. return false;
  703. *id = obs->service_types.array[idx].id;
  704. return true;
  705. }
  706. void obs_enter_graphics(void)
  707. {
  708. if (obs && obs->video.graphics)
  709. gs_enter_context(obs->video.graphics);
  710. }
  711. void obs_leave_graphics(void)
  712. {
  713. if (obs && obs->video.graphics)
  714. gs_leave_context();
  715. }
  716. audio_t *obs_get_audio(void)
  717. {
  718. return (obs != NULL) ? obs->audio.audio : NULL;
  719. }
  720. video_t *obs_get_video(void)
  721. {
  722. return (obs != NULL) ? obs->video.video : NULL;
  723. }
  724. /* TODO: optimize this later so it's not just O(N) string lookups */
  725. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  726. const char *task, const char *target)
  727. {
  728. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  729. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  730. if (strcmp(callback->id, id) == 0 &&
  731. strcmp(callback->task, task) == 0 &&
  732. strcmp(callback->target, target) == 0)
  733. return callback;
  734. }
  735. return NULL;
  736. }
  737. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  738. const char *task, const char *target)
  739. {
  740. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  741. struct obs_modeless_ui *callback;
  742. callback = obs->modeless_ui_callbacks.array+i;
  743. if (strcmp(callback->id, id) == 0 &&
  744. strcmp(callback->task, task) == 0 &&
  745. strcmp(callback->target, target) == 0)
  746. return callback;
  747. }
  748. return NULL;
  749. }
  750. int obs_exec_ui(const char *name, const char *task, const char *target,
  751. void *data, void *ui_data)
  752. {
  753. struct obs_modal_ui *callback;
  754. int errorcode = OBS_UI_NOTFOUND;
  755. if (!obs) return errorcode;
  756. callback = get_modal_ui_callback(name, task, target);
  757. if (callback) {
  758. bool success = callback->exec(data, ui_data);
  759. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  760. }
  761. return errorcode;
  762. }
  763. void *obs_create_ui(const char *name, const char *task, const char *target,
  764. void *data, void *ui_data)
  765. {
  766. struct obs_modeless_ui *callback;
  767. if (!obs) return NULL;
  768. callback = get_modeless_ui_callback(name, task, target);
  769. return callback ? callback->create(data, ui_data) : NULL;
  770. }
  771. bool obs_add_source(obs_source_t *source)
  772. {
  773. struct calldata params = {0};
  774. if (!obs) return false;
  775. if (!source) return false;
  776. pthread_mutex_lock(&obs->data.sources_mutex);
  777. da_push_back(obs->data.user_sources, &source);
  778. obs_source_addref(source);
  779. pthread_mutex_unlock(&obs->data.sources_mutex);
  780. calldata_set_ptr(&params, "source", source);
  781. signal_handler_signal(obs->signals, "source_add", &params);
  782. calldata_free(&params);
  783. return true;
  784. }
  785. obs_source_t *obs_get_output_source(uint32_t channel)
  786. {
  787. if (!obs) return NULL;
  788. return obs_view_get_source(&obs->data.main_view, channel);
  789. }
  790. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  791. {
  792. assert(channel < MAX_CHANNELS);
  793. if (!obs) return;
  794. if (channel >= MAX_CHANNELS) return;
  795. struct obs_source *prev_source;
  796. struct obs_view *view = &obs->data.main_view;
  797. struct calldata params = {0};
  798. pthread_mutex_lock(&view->channels_mutex);
  799. obs_source_addref(source);
  800. prev_source = view->channels[channel];
  801. calldata_set_int(&params, "channel", channel);
  802. calldata_set_ptr(&params, "prev_source", prev_source);
  803. calldata_set_ptr(&params, "source", source);
  804. signal_handler_signal(obs->signals, "channel_change", &params);
  805. calldata_get_ptr(&params, "source", &source);
  806. calldata_free(&params);
  807. view->channels[channel] = source;
  808. pthread_mutex_unlock(&view->channels_mutex);
  809. if (source)
  810. obs_source_activate(source, MAIN_VIEW);
  811. if (prev_source) {
  812. obs_source_deactivate(prev_source, MAIN_VIEW);
  813. obs_source_release(prev_source);
  814. }
  815. }
  816. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
  817. {
  818. if (!obs) return;
  819. pthread_mutex_lock(&obs->data.user_sources_mutex);
  820. for (size_t i = 0; i < obs->data.user_sources.num; i++) {
  821. struct obs_source *source = obs->data.user_sources.array[i];
  822. if (!enum_proc(param, source))
  823. break;
  824. }
  825. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  826. }
  827. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  828. void *param)
  829. {
  830. struct obs_context_data **start = pstart, *context;
  831. bool (*enum_proc)(void*, void*) = proc;
  832. assert(start);
  833. assert(mutex);
  834. assert(enum_proc);
  835. pthread_mutex_lock(mutex);
  836. context = *start;
  837. while (context) {
  838. if (!enum_proc(param, context))
  839. break;
  840. context = context->next;
  841. }
  842. pthread_mutex_unlock(mutex);
  843. }
  844. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
  845. {
  846. if (!obs) return;
  847. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  848. enum_proc, param);
  849. }
  850. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
  851. {
  852. if (!obs) return;
  853. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  854. enum_proc, param);
  855. }
  856. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
  857. {
  858. if (!obs) return;
  859. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  860. enum_proc, param);
  861. }
  862. obs_source_t *obs_get_source_by_name(const char *name)
  863. {
  864. struct obs_core_data *data = &obs->data;
  865. struct obs_source *source = NULL;
  866. size_t i;
  867. if (!obs) return NULL;
  868. pthread_mutex_lock(&data->user_sources_mutex);
  869. for (i = 0; i < data->user_sources.num; i++) {
  870. struct obs_source *cur_source = data->user_sources.array[i];
  871. if (strcmp(cur_source->context.name, name) == 0) {
  872. source = cur_source;
  873. obs_source_addref(source);
  874. break;
  875. }
  876. }
  877. pthread_mutex_unlock(&data->user_sources_mutex);
  878. return source;
  879. }
  880. static inline void *get_context_by_name(void *vfirst, const char *name,
  881. pthread_mutex_t *mutex)
  882. {
  883. struct obs_context_data **first = vfirst;
  884. struct obs_context_data *context;
  885. pthread_mutex_lock(mutex);
  886. context = *first;
  887. while (context) {
  888. if (strcmp(context->name, name) == 0)
  889. break;
  890. context = context->next;
  891. }
  892. pthread_mutex_unlock(mutex);
  893. return context;
  894. }
  895. obs_output_t *obs_get_output_by_name(const char *name)
  896. {
  897. if (!obs) return NULL;
  898. return get_context_by_name(&obs->data.first_output, name,
  899. &obs->data.outputs_mutex);
  900. }
  901. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  902. {
  903. if (!obs) return NULL;
  904. return get_context_by_name(&obs->data.first_encoder, name,
  905. &obs->data.encoders_mutex);
  906. }
  907. obs_service_t *obs_get_service_by_name(const char *name)
  908. {
  909. if (!obs) return NULL;
  910. return get_context_by_name(&obs->data.first_service, name,
  911. &obs->data.services_mutex);
  912. }
  913. gs_effect_t *obs_get_default_effect(void)
  914. {
  915. if (!obs) return NULL;
  916. return obs->video.default_effect;
  917. }
  918. gs_effect_t *obs_get_default_rect_effect(void)
  919. {
  920. if (!obs) return NULL;
  921. return obs->video.default_rect_effect;
  922. }
  923. gs_effect_t *obs_get_opaque_effect(void)
  924. {
  925. if (!obs) return NULL;
  926. return obs->video.opaque_effect;
  927. }
  928. gs_effect_t *obs_get_solid_effect(void)
  929. {
  930. if (!obs) return NULL;
  931. return obs->video.solid_effect;
  932. }
  933. signal_handler_t *obs_get_signal_handler(void)
  934. {
  935. if (!obs) return NULL;
  936. return obs->signals;
  937. }
  938. proc_handler_t *obs_get_proc_handler(void)
  939. {
  940. if (!obs) return NULL;
  941. return obs->procs;
  942. }
  943. void obs_add_draw_callback(
  944. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  945. void *param)
  946. {
  947. if (!obs) return;
  948. obs_display_add_draw_callback(&obs->video.main_display, draw, param);
  949. }
  950. void obs_remove_draw_callback(
  951. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  952. void *param)
  953. {
  954. if (!obs) return;
  955. obs_display_remove_draw_callback(&obs->video.main_display, draw, param);
  956. }
  957. void obs_resize(uint32_t cx, uint32_t cy)
  958. {
  959. if (!obs || !obs->video.video || !obs->video.graphics) return;
  960. obs_display_resize(&obs->video.main_display, cx, cy);
  961. }
  962. void obs_render_main_view(void)
  963. {
  964. if (!obs) return;
  965. obs_view_render(&obs->data.main_view);
  966. }
  967. void obs_set_master_volume(float volume)
  968. {
  969. struct calldata data = {0};
  970. if (!obs) return;
  971. calldata_set_float(&data, "volume", volume);
  972. signal_handler_signal(obs->signals, "master_volume", &data);
  973. volume = (float)calldata_float(&data, "volume");
  974. calldata_free(&data);
  975. obs->audio.user_volume = volume;
  976. }
  977. void obs_set_present_volume(float volume)
  978. {
  979. if (!obs) return;
  980. obs->audio.present_volume = volume;
  981. }
  982. float obs_get_master_volume(void)
  983. {
  984. return obs ? obs->audio.user_volume : 0.0f;
  985. }
  986. float obs_get_present_volume(void)
  987. {
  988. return obs ? obs->audio.present_volume : 0.0f;
  989. }
  990. static obs_source_t *obs_load_source_type(obs_data_t *source_data,
  991. enum obs_source_type type)
  992. {
  993. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  994. obs_source_t *source;
  995. const char *name = obs_data_get_string(source_data, "name");
  996. const char *id = obs_data_get_string(source_data, "id");
  997. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  998. double volume;
  999. int64_t sync;
  1000. uint32_t flags;
  1001. uint32_t mixers;
  1002. source = obs_source_create(type, id, name, settings);
  1003. obs_data_set_default_double(source_data, "volume", 1.0);
  1004. volume = obs_data_get_double(source_data, "volume");
  1005. obs_source_set_volume(source, (float)volume);
  1006. sync = obs_data_get_int(source_data, "sync");
  1007. obs_source_set_sync_offset(source, sync);
  1008. obs_data_set_default_int(source_data, "mixers", 0xF);
  1009. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1010. obs_source_set_audio_mixers(source, mixers);
  1011. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1012. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1013. obs_source_set_flags(source, flags);
  1014. obs_data_set_default_bool(source_data, "enabled", true);
  1015. obs_source_set_enabled(source,
  1016. obs_data_get_bool(source_data, "enabled"));
  1017. if (filters) {
  1018. size_t count = obs_data_array_count(filters);
  1019. for (size_t i = 0; i < count; i++) {
  1020. obs_data_t *filter_data =
  1021. obs_data_array_item(filters, i);
  1022. obs_source_t *filter = obs_load_source_type(
  1023. filter_data, OBS_SOURCE_TYPE_FILTER);
  1024. if (filter) {
  1025. obs_source_filter_add(source, filter);
  1026. obs_source_release(filter);
  1027. }
  1028. obs_data_release(filter_data);
  1029. }
  1030. obs_data_array_release(filters);
  1031. }
  1032. obs_data_release(settings);
  1033. return source;
  1034. }
  1035. obs_source_t *obs_load_source(obs_data_t *source_data)
  1036. {
  1037. return obs_load_source_type(source_data, OBS_SOURCE_TYPE_INPUT);
  1038. }
  1039. void obs_load_sources(obs_data_array_t *array)
  1040. {
  1041. size_t count;
  1042. size_t i;
  1043. if (!obs) return;
  1044. count = obs_data_array_count(array);
  1045. pthread_mutex_lock(&obs->data.user_sources_mutex);
  1046. for (i = 0; i < count; i++) {
  1047. obs_data_t *source_data = obs_data_array_item(array, i);
  1048. obs_source_t *source = obs_load_source(source_data);
  1049. obs_add_source(source);
  1050. obs_source_release(source);
  1051. obs_data_release(source_data);
  1052. }
  1053. /* tell sources that we want to load */
  1054. for (i = 0; i < obs->data.user_sources.num; i++)
  1055. obs_source_load(obs->data.user_sources.array[i]);
  1056. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  1057. }
  1058. obs_data_t *obs_save_source(obs_source_t *source)
  1059. {
  1060. obs_data_array_t *filters = obs_data_array_create();
  1061. obs_data_t *source_data = obs_data_create();
  1062. obs_data_t *settings = obs_source_get_settings(source);
  1063. float volume = obs_source_get_volume(source);
  1064. uint32_t mixers = obs_source_get_audio_mixers(source);
  1065. int64_t sync = obs_source_get_sync_offset(source);
  1066. uint32_t flags = obs_source_get_flags(source);
  1067. const char *name = obs_source_get_name(source);
  1068. const char *id = obs_source_get_id(source);
  1069. bool enabled = obs_source_enabled(source);
  1070. obs_source_save(source);
  1071. obs_data_set_string(source_data, "name", name);
  1072. obs_data_set_string(source_data, "id", id);
  1073. obs_data_set_obj (source_data, "settings", settings);
  1074. obs_data_set_int (source_data, "mixers", mixers);
  1075. obs_data_set_int (source_data, "sync", sync);
  1076. obs_data_set_int (source_data, "flags", flags);
  1077. obs_data_set_double(source_data, "volume", volume);
  1078. obs_data_set_bool (source_data, "enabled", enabled);
  1079. pthread_mutex_lock(&source->filter_mutex);
  1080. if (source->filters.num) {
  1081. for (size_t i = source->filters.num; i > 0; i--) {
  1082. obs_source_t *filter = source->filters.array[i - 1];
  1083. obs_data_t *filter_data = obs_save_source(filter);
  1084. obs_data_array_push_back(filters, filter_data);
  1085. obs_data_release(filter_data);
  1086. }
  1087. obs_data_set_array(source_data, "filters", filters);
  1088. }
  1089. pthread_mutex_unlock(&source->filter_mutex);
  1090. obs_data_release(settings);
  1091. obs_data_array_release(filters);
  1092. return source_data;
  1093. }
  1094. obs_data_array_t *obs_save_sources(void)
  1095. {
  1096. obs_data_array_t *array;
  1097. size_t i;
  1098. if (!obs) return NULL;
  1099. array = obs_data_array_create();
  1100. pthread_mutex_lock(&obs->data.user_sources_mutex);
  1101. for (i = 0; i < obs->data.user_sources.num; i++) {
  1102. obs_source_t *source = obs->data.user_sources.array[i];
  1103. obs_data_t *source_data = obs_save_source(source);
  1104. obs_data_array_push_back(array, source_data);
  1105. obs_data_release(source_data);
  1106. }
  1107. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  1108. return array;
  1109. }
  1110. /* ensures that names are never blank */
  1111. static inline char *dup_name(const char *name)
  1112. {
  1113. if (!name || !*name) {
  1114. struct dstr unnamed = {0};
  1115. dstr_printf(&unnamed, "__unnamed%004lld",
  1116. obs->data.unnamed_index++);
  1117. return unnamed.array;
  1118. } else {
  1119. return bstrdup(name);
  1120. }
  1121. }
  1122. static inline bool obs_context_data_init_wrap(
  1123. struct obs_context_data *context,
  1124. obs_data_t *settings,
  1125. const char *name)
  1126. {
  1127. assert(context);
  1128. memset(context, 0, sizeof(*context));
  1129. pthread_mutex_init_value(&context->rename_cache_mutex);
  1130. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1131. return false;
  1132. context->signals = signal_handler_create();
  1133. if (!context->signals)
  1134. return false;
  1135. context->procs = proc_handler_create();
  1136. if (!context->procs)
  1137. return false;
  1138. context->name = dup_name(name);
  1139. context->settings = obs_data_newref(settings);
  1140. return true;
  1141. }
  1142. bool obs_context_data_init(
  1143. struct obs_context_data *context,
  1144. obs_data_t *settings,
  1145. const char *name)
  1146. {
  1147. if (obs_context_data_init_wrap(context, settings, name)) {
  1148. return true;
  1149. } else {
  1150. obs_context_data_free(context);
  1151. return false;
  1152. }
  1153. }
  1154. void obs_context_data_free(struct obs_context_data *context)
  1155. {
  1156. signal_handler_destroy(context->signals);
  1157. proc_handler_destroy(context->procs);
  1158. obs_data_release(context->settings);
  1159. obs_context_data_remove(context);
  1160. pthread_mutex_destroy(&context->rename_cache_mutex);
  1161. bfree(context->name);
  1162. for (size_t i = 0; i < context->rename_cache.num; i++)
  1163. bfree(context->rename_cache.array[i]);
  1164. da_free(context->rename_cache);
  1165. memset(context, 0, sizeof(*context));
  1166. }
  1167. void obs_context_data_insert(struct obs_context_data *context,
  1168. pthread_mutex_t *mutex, void *pfirst)
  1169. {
  1170. struct obs_context_data **first = pfirst;
  1171. assert(context);
  1172. assert(mutex);
  1173. assert(first);
  1174. context->mutex = mutex;
  1175. pthread_mutex_lock(mutex);
  1176. context->prev_next = first;
  1177. context->next = *first;
  1178. *first = context;
  1179. if (context->next)
  1180. context->next->prev_next = &context->next;
  1181. pthread_mutex_unlock(mutex);
  1182. }
  1183. void obs_context_data_remove(struct obs_context_data *context)
  1184. {
  1185. if (context && context->mutex) {
  1186. pthread_mutex_lock(context->mutex);
  1187. if (context->prev_next)
  1188. *context->prev_next = context->next;
  1189. if (context->next)
  1190. context->next->prev_next = context->prev_next;
  1191. pthread_mutex_unlock(context->mutex);
  1192. context->mutex = NULL;
  1193. }
  1194. }
  1195. void obs_context_data_setname(struct obs_context_data *context,
  1196. const char *name)
  1197. {
  1198. pthread_mutex_lock(&context->rename_cache_mutex);
  1199. if (context->name)
  1200. da_push_back(context->rename_cache, &context->name);
  1201. context->name = dup_name(name);
  1202. pthread_mutex_unlock(&context->rename_cache_mutex);
  1203. }