obs.c 44 KB

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