Skip to content

Commit d33d923

Browse files
committed
aplay/wasapi: use get_name from acap
+ rename the fn
1 parent adf3993 commit d33d923

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

src/audio/capture/wasapi.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct state_acap_wasapi {
9797

9898
enum { IDX_LOOP = -2, IDX_DFL = -1 };
9999

100-
static string get_name(IMMDevice *pDevice);
100+
string wasapi_get_name(IMMDevice *pDevice);
101101
static void show_help();
102102
string wasapi_get_default_device_id(EDataFlow dataFlow, IMMDeviceEnumerator *enumerator);
103103

@@ -134,7 +134,11 @@ static void audio_cap_wasapi_probe(struct device_info **available_devices, int *
134134
*available_devices = (struct device_info *) realloc(*available_devices, (*dev_count + 1) * sizeof(struct device_info));
135135
memset(&(*available_devices)[*dev_count], 0, sizeof(struct device_info));
136136
snprintf((*available_devices)[*dev_count].dev, sizeof (*available_devices)[*dev_count].dev, ":%u", i); ///< @todo This may be rather id than index
137-
snprintf((*available_devices)[*dev_count].name, sizeof (*available_devices)[*dev_count].name, "WASAPI %s", get_name(pDevice).c_str());
137+
snprintf(
138+
(*available_devices)[*dev_count].name,
139+
sizeof(*available_devices)[*dev_count].name,
140+
"WASAPI %s", wasapi_get_name(pDevice).c_str());
141+
138142
++*dev_count;
139143
} catch (ug_runtime_error &e) {
140144
LOG(LOG_LEVEL_WARNING) << MOD_NAME << "Device " << i << ": " << e.what() << "\n";
@@ -158,7 +162,7 @@ static void audio_cap_wasapi_probe(struct device_info **available_devices, int *
158162
*dev_count += 1;
159163
}
160164

161-
static string get_name(IMMDevice *pDevice) {
165+
string wasapi_get_name(IMMDevice *pDevice) {
162166
wstring out;
163167
IPropertyStore *pProps = NULL;
164168
PROPVARIANT varName;
@@ -224,7 +228,8 @@ static void show_help() {
224228
"%s\t" TBOLD("%2d") ") " TBOLD(
225229
"%s") " (ID: %s)\n",
226230
(dev_id == default_dev_id ? "(*)" : ""), i,
227-
get_name(pDevice).c_str(), dev_id.c_str());
231+
wasapi_get_name(pDevice).c_str(),
232+
dev_id.c_str());
228233
} catch (ug_runtime_error &e) {
229234
LOG(LOG_LEVEL_WARNING) << MOD_NAME << "Device " << i << ": " << e.what() << "\n";
230235
}
@@ -309,8 +314,9 @@ static void * audio_cap_wasapi_init(struct module *parent, const char *cfg)
309314
IMMDevice *pDevice = nullptr;
310315
pEndpoints->Item(i, &pDevice);
311316
if (pDevice != nullptr &&
312-
get_name(pDevice).find(
313-
req_dev_name) !=
317+
wasapi_get_name(pDevice)
318+
.find(
319+
req_dev_name) !=
314320
std::string::npos) {
315321
s->pDevice = pDevice;
316322
break;
@@ -333,7 +339,7 @@ static void * audio_cap_wasapi_init(struct module *parent, const char *cfg)
333339
THROW_IF_FAILED(s->pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL,
334340
(void **)&s->pAudioClient));
335341

336-
auto friendlyName = get_name(s->pDevice);
342+
auto friendlyName = wasapi_get_name(s->pDevice);
337343
if (!friendlyName.empty()) {
338344
LOG(LOG_LEVEL_NOTICE) << MOD_NAME << "Using device: "
339345
<< friendlyName << "\n";

src/audio/playback/wasapi.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
7070
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
7171
const IID IID_IAudioClient = __uuidof(IAudioClient);
7272
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
73-
const static GUID IDevice_FriendlyName = { 0xa45c254e, 0xdf1c, 0x4efd, { 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0 } };
74-
const static PROPERTYKEY PKEY_Device_FriendlyName = { IDevice_FriendlyName, 14 };
7573
#pragma GCC diagnostic push
7674
#pragma GCC diagnostic ignored "-Wmissing-braces" // not our issue - defined by Mingw-w64
7775
const static GUID UG_KSDATAFORMAT_SUBTYPE_PCM = { STATIC_KSDATAFORMAT_SUBTYPE_PCM };
@@ -94,32 +92,13 @@ struct state_aplay_wasapi {
9492
};
9593

9694
string wasapi_get_default_device_id(EDataFlow dataFlow, IMMDeviceEnumerator *enumerator); // defined in WASAPI capture
95+
string wasapi_get_name(IMMDevice *pDevice); // defined in audio/playback/wasapi.cpp
9796

9897
#define SAFE_RELEASE(u) \
9998
do { if ((u) != NULL) (u)->Release(); (u) = NULL; } while(0)
10099
#undef THROW_IF_FAILED
101100
#define THROW_IF_FAILED(cmd) do { HRESULT hr = cmd; if (!SUCCEEDED(hr)) { ostringstream oss; oss << #cmd << ": " << hresult_to_str(hr); throw ug_runtime_error(oss.str()); } } while(0)
102101

103-
static string get_name(IMMDevice *pDevice) {
104-
wstring out;
105-
IPropertyStore *pProps = NULL;
106-
PROPVARIANT varName;
107-
LPWSTR pwszID = NULL;
108-
// Initialize container for property value.
109-
PropVariantInit(&varName);
110-
HRESULT hr = pDevice->OpenPropertyStore(STGM_READ, &pProps);
111-
if (!FAILED(hr)) {
112-
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
113-
if (!FAILED(hr)) {
114-
out = varName.pwszVal;
115-
}
116-
}
117-
SAFE_RELEASE(pProps);
118-
CoTaskMemFree(pwszID);
119-
PropVariantClear(&varName);
120-
return win_wstr_to_str(out.c_str());
121-
}
122-
123102
static void audio_play_wasapi_probe(struct device_info **available_devices, int *dev_count, void (**deleter)(void *))
124103
{
125104
*deleter = free;
@@ -149,7 +128,11 @@ static void audio_play_wasapi_probe(struct device_info **available_devices, int
149128
*available_devices = (struct device_info *) realloc(*available_devices, (*dev_count + 1) * sizeof(struct device_info));
150129
memset(&(*available_devices)[*dev_count], 0, sizeof(struct device_info));
151130
snprintf((*available_devices)[*dev_count].dev, sizeof (*available_devices)[*dev_count].dev, ":%u", i); ///< @todo This may be rather id than index
152-
snprintf((*available_devices)[*dev_count].name, sizeof (*available_devices)[*dev_count].name, "WASAPI %s", get_name(pDevice).c_str());
131+
snprintf(
132+
(*available_devices)[*dev_count].name,
133+
sizeof(*available_devices)[*dev_count].name,
134+
"WASAPI %s",
135+
wasapi_get_name(pDevice).c_str());
153136
++*dev_count;
154137
} catch (ug_runtime_error &e) {
155138
LOG(LOG_LEVEL_WARNING) << MOD_NAME << "Device " << i << ": " << e.what() << "\n";
@@ -193,7 +176,10 @@ static void audio_play_wasapi_help() {
193176
THROW_IF_FAILED(pEndpoints->Item(i, &pDevice));
194177
THROW_IF_FAILED(pDevice->GetId(&pwszID));
195178
string dev_id = win_wstr_to_str(pwszID);
196-
col() << (dev_id == default_dev_id ? "(*)" : "") << "\t" << SBOLD(i) << ") " << SBOLD(get_name(pDevice)) << " (ID: " << dev_id << ")\n";
179+
col() << (dev_id == default_dev_id ? "(*)" : "")
180+
<< "\t" << SBOLD(i) << ") "
181+
<< SBOLD(wasapi_get_name(pDevice))
182+
<< " (ID: " << dev_id << ")\n";
197183
} catch (ug_runtime_error &e) {
198184
LOG(LOG_LEVEL_WARNING) << MOD_NAME << "Device " << i << ": " << e.what() << "\n";
199185
}
@@ -276,8 +262,9 @@ audio_play_wasapi_init(const struct audio_playback_opts *opts)
276262
IMMDevice *pDevice = nullptr;
277263
pEndpoints->Item(i, &pDevice);
278264
if (pDevice != nullptr &&
279-
get_name(pDevice).find(
280-
req_dev_name) !=
265+
wasapi_get_name(pDevice)
266+
.find(
267+
req_dev_name) !=
281268
std::string::npos) {
282269
s->pDevice = pDevice;
283270
break;
@@ -298,7 +285,7 @@ audio_play_wasapi_init(const struct audio_playback_opts *opts)
298285
THROW_IF_FAILED(s->pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL,
299286
(void **)&s->pAudioClient));
300287

301-
auto friendlyName = get_name(s->pDevice);
288+
auto friendlyName = wasapi_get_name(s->pDevice);
302289
if (!friendlyName.empty()) {
303290
LOG(LOG_LEVEL_NOTICE) << MOD_NAME << "Using device: "
304291
<< friendlyName << "\n";

0 commit comments

Comments
 (0)