Skip to content

Commit 00fb576

Browse files
committed
wasapi: fix setting dev by UUID
The mutibyte to wide character conversion was (perhaps since the beginning) wrong - mbtowc converts just one character - it should have beem mbstowcs but using rather mbsrttowcs (thread-safe).
1 parent be2b3dc commit 00fb576

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/audio/capture/wasapi.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
*/
2727

2828

29+
#include <assert.h> // for assert
2930
#include <audiosessiontypes.h> // for AUDCLNT_STREAMFLAGS_LOOPBACK, _AUDC...
3031
#include <audioclient.h>
3132
#include <basetsd.h> // for HRESULT, UINT, LPWSTR, GUID, UINT32
3233
#include <cctype> // for isdigit
3334
#include <cstdio> // for mesnprintf
34-
#include <cstdlib> // for realloc, atoi, malloc, mbtowc, free
35+
#include <cstdlib> // for realloc, atoi, malloc, free
3536
#include <cstring> // for NULL, memset, strcmp, strlen, wcslen
3637
#include <combaseapi.h> // for CoTaskMemFree, CoCreateInstance
38+
#include <cwchar> // for mbsrtowcs
3739
#include <guiddef.h> // for IsEqualGUID
3840
#include <iomanip>
3941
#include <iostream>
@@ -248,7 +250,12 @@ static void * audio_cap_wasapi_init(struct module *parent, const char *cfg)
248250
} else if (strcmp(cfg, "loopback") == 0) {
249251
index = IDX_LOOP;
250252
} else {
251-
mbtowc(deviceID, cfg, (sizeof deviceID / 2) - 1);
253+
const char *uuid = cfg;
254+
mbstate_t state{};
255+
mbsrtowcs(deviceID, &uuid,
256+
(sizeof deviceID / sizeof deviceID[0]) - 1,
257+
&state);
258+
assert(uuid == NULL);
252259
}
253260
}
254261
auto s = new state_acap_wasapi();

src/audio/playback/wasapi.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
#include <cctype> // for isdigit
3636
#include <combaseapi.h> // for CoTaskMemFree, CoCreateInstance
3737
#include <cstdio> // for snprintf
38-
#include <cstdlib> // for NULL, atoi, malloc, mbtowc, realloc
38+
#include <cstdlib> // for NULL, atoi, malloc, realloc
3939
#include <cstring> // for memset, strcmp, strlen, wcslen
40+
#include <cwchar> // for mbsrtowcs
4041
#include <iostream>
4142
#include <ksmedia.h> // for KSAUDIO_SPEAKER_5POINT1_SURROUND
4243
#include <mediaobj.h> // for REFERENCE_TIME
@@ -218,7 +219,12 @@ audio_play_wasapi_init(const struct audio_playback_opts *opts)
218219
if (isdigit(opts->cfg[0])) {
219220
index = atoi(opts->cfg);
220221
} else {
221-
mbtowc(deviceID, opts->cfg, (sizeof deviceID / 2) - 1);
222+
const char *uuid = opts->cfg;
223+
mbstate_t state{};
224+
mbsrtowcs(deviceID, &uuid,
225+
(sizeof deviceID / sizeof deviceID[0]) - 1,
226+
&state);
227+
assert(uuid == NULL);
222228
}
223229
}
224230
auto s = new state_aplay_wasapi();

0 commit comments

Comments
 (0)