Skip to content

Commit fb3e152

Browse files
committed
aplay/wasapi: allow specification by name
1 parent 6e32acc commit fb3e152

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/audio/playback/wasapi.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ static void audio_play_wasapi_probe(struct device_info **available_devices, int
165165
}
166166

167167
static void audio_play_wasapi_help() {
168-
col() << "Usage:\n" <<
169-
SBOLD(SRED("\t-r wasapi") << "[:<index>|:<ID>] --param audio-buffer-len=<ms>") << "\n" <<
170-
"\nAvailable devices:\n";
168+
col()
169+
<< "Usage:\n"
170+
<< SBOLD(
171+
SRED("\t-r wasapi")
172+
<< "[:<index>|:<ID>|:<name>] --param audio-buffer-len=<ms>")
173+
<< "\n"
174+
<< "\nAvailable devices:\n";
171175

172176
bool com_initialized = false;
173177
if (!com_initialize(&com_initialized, MOD_NAME)) {
@@ -203,6 +207,7 @@ static void audio_play_wasapi_help() {
203207
SAFE_RELEASE(enumerator);
204208
SAFE_RELEASE(pEndpoints);
205209
com_uninitialize(&com_initialized);
210+
printf("\nDevice " TBOLD("name") " can be a substring (selects first matching device).\n");
206211
}
207212

208213
static void *
@@ -213,18 +218,22 @@ audio_play_wasapi_init(const struct audio_playback_opts *opts)
213218
return INIT_NOERR;
214219
}
215220

216-
wchar_t deviceID[1024] = L"";
217-
int index = -1;
221+
int index = -1; // or:
222+
wchar_t deviceID[1024] = L""; // or:
223+
char req_dev_name[1024] = "";
224+
218225
if (strlen(opts->cfg) > 0) {
219226
if (isdigit(opts->cfg[0])) {
220227
index = atoi(opts->cfg);
221-
} else {
228+
} else if (opts->cfg[0] == '{') { // ID
222229
const char *uuid = opts->cfg;
223230
mbstate_t state{};
224231
mbsrtowcs(deviceID, &uuid,
225232
(sizeof deviceID / sizeof deviceID[0]) - 1,
226233
&state);
227234
assert(uuid == NULL);
235+
} else { // name
236+
snprintf_ch(req_dev_name, "%s", opts->cfg);
228237
}
229238
}
230239
auto s = new state_aplay_wasapi();
@@ -239,7 +248,7 @@ audio_play_wasapi_init(const struct audio_playback_opts *opts)
239248
(void **) &enumerator));
240249
if (wcslen(deviceID) > 0) {
241250
THROW_IF_FAILED(enumerator->GetDevice(deviceID, &s->pDevice));
242-
} else if (index != -1) {
251+
} else if (index != -1 || strlen(req_dev_name) > 0) {
243252
IMMDeviceCollection *pEndpoints = nullptr;
244253
try {
245254
THROW_IF_FAILED(enumerator->EnumAudioEndpoints(eRender, DEVICE_STATEMASK_ALL, &pEndpoints));
@@ -250,6 +259,18 @@ audio_play_wasapi_init(const struct audio_playback_opts *opts)
250259
THROW_IF_FAILED(pEndpoints->Item(i, &s->pDevice));
251260
break;
252261
}
262+
if (strlen(req_dev_name) > 0) {
263+
IMMDevice *pDevice = nullptr;
264+
pEndpoints->Item(i, &pDevice);
265+
if (pDevice != nullptr &&
266+
get_name(pDevice).find(
267+
req_dev_name) !=
268+
std::string::npos) {
269+
s->pDevice = pDevice;
270+
break;
271+
}
272+
SAFE_RELEASE(pDevice);
273+
}
253274
}
254275
} catch (ug_runtime_error &e) { // just continue with the next
255276
LOG(LOG_LEVEL_WARNING) << MOD_NAME << e.what() << "\n";

0 commit comments

Comments
 (0)