Skip to content

Commit 1edca38

Browse files
committed
Fixed audio device names on mac #28
1 parent 9d268c6 commit 1edca38

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

webrtc-jni/src/main/cpp/include/platform/macos/MacUtils.h

+5
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ inline void ThrowIfFailed(OSStatus status, const char * msg, ...)
3838
}
3939
}
4040

41+
namespace jni
42+
{
43+
const std::string CFStringRefToUTF8(CFStringRef stringRef);
44+
}
45+
4146
#endif

webrtc-jni/src/main/cpp/src/media/audio/macos/CoreAudioDeviceManager.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ namespace jni
275275

276276
AudioDevicePtr CoreAudioDeviceManager::createAudioDevice(const AudioDeviceID & deviceID, const AudioObjectPropertyScope & scope) {
277277
AudioDevicePtr device = nullptr;
278-
CFStringRef devNameRef;
278+
CFStringRef devNameRef = nullptr;
279279
UInt32 dataSize = sizeof(devNameRef);
280280

281281
AudioObjectPropertyAddress pa;
@@ -286,16 +286,11 @@ namespace jni
286286
OSStatus status = AudioObjectGetPropertyData(deviceID, &pa, 0, nullptr, &dataSize, &devNameRef);
287287
ThrowIfFailed(status, "CoreAudio: Get device name failed");
288288

289-
CFIndex length = CFStringGetLength(devNameRef) + 1;
290-
291-
char deviceName[length];
289+
std::string name = CFStringRefToUTF8(devNameRef);
290+
std::string id = std::to_string(deviceID);
292291

293-
CFStringGetCString(devNameRef, deviceName, length, kCFStringEncodingUTF8);
294292
CFRelease(devNameRef);
295293

296-
std::string name(deviceName, length);
297-
std::string id = std::to_string(deviceID);
298-
299294
unsigned channels = getChannelCount(deviceID, scope);
300295

301296
if (channels > 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2022 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "platform/macos/MacUtils.h"
18+
19+
#include <vector>
20+
21+
namespace jni
22+
{
23+
const std::string CFStringRefToUTF8(CFStringRef stringRef) {
24+
CFIndex length = CFStringGetLength(stringRef);
25+
26+
if (length == 0) {
27+
return std::string();
28+
}
29+
30+
CFRange range = CFRangeMake(0, length);
31+
CFIndex outputSize;
32+
CFIndex converted = CFStringGetBytes(stringRef, range, kCFStringEncodingUTF8, 0, false, nullptr, 0, &outputSize);
33+
34+
if (converted == 0 || outputSize <= 0) {
35+
return std::string();
36+
}
37+
38+
size_t elements = static_cast<size_t>(outputSize) * sizeof(UInt8) / sizeof(char) + 1;
39+
40+
std::vector<char> buffer(elements);
41+
42+
converted = CFStringGetBytes(stringRef, range, kCFStringEncodingUTF8, 0, false,
43+
reinterpret_cast<UInt8*>(&buffer[0]), outputSize, nullptr);
44+
45+
if (converted == 0) {
46+
return std::string();
47+
}
48+
49+
// Terminate string.
50+
buffer[elements - 1] = '\0';
51+
52+
return std::string(&buffer[0], elements - 1);
53+
}
54+
}

0 commit comments

Comments
 (0)