From 6dd65c8d134936ee8eeaf70f8913172ccf34c99c Mon Sep 17 00:00:00 2001 From: jp9000 Date: Tue, 8 Mar 2022 01:02:44 -0800 Subject: [PATCH] UI: Fix duplicated source names in audio settings When you add an audio capture source and use the "use existing" feature in the source selection dialog, and then recreate the same audio source again in audio settings, it will result in two sources using identical names, which although relatively harmless can cause issues when doing things such as trying to find sources by their name. Fixes obsproject/obs-studio#5621 Closes obsproject/obs-studio#5947 --- UI/window-basic-main.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp index 4693ed08c..9e77a9f32 100644 --- a/UI/window-basic-main.cpp +++ b/UI/window-basic-main.cpp @@ -4412,6 +4412,25 @@ bool OBSBasic::ResetAudio() return obs_reset_audio(&ai); } +extern std::string strprintf(const char *format, ...); + +static std::string GetDupName(const char *name) +{ + std::string newName = name; + int inc = 1; + + for (;;) { + OBSSourceAutoRelease existing_source = + obs_get_source_by_name(newName.c_str()); + if (!existing_source) + break; + + newName = strprintf("%s (%d)", name, ++inc); + } + + return newName; +} + void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId, const char *deviceDesc, int channel) { @@ -4435,9 +4454,11 @@ void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId, } } else if (!disable) { + std::string name = GetDupName(deviceDesc); + settings = obs_data_create(); obs_data_set_string(settings, "device_id", deviceId); - source = obs_source_create(sourceId, deviceDesc, settings, + source = obs_source_create(sourceId, name.c_str(), settings, nullptr); obs_set_output_source(channel, source);