obs.c 39 KB

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