obs.c 35 KB

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