Skip to content

Commit 1b57b59

Browse files
author
Vladimir Morosev
committed
Audio device selection for external Audio Device Module componenet
1 parent fa2dda4 commit 1b57b59

4 files changed

+58
-28
lines changed

idl/WebRtcFactory.idl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ namespace org
108108
/// Gets or sets if audio rendering is enabled.
109109
/// </summary>
110110
bool audioRenderingEnabled = true;
111+
112+
/// <summary>
113+
/// Gets or sets Device ID of used audio capture device.
114+
/// </summary>
115+
string audioCaptureDeviceId;
116+
117+
/// <summary>
118+
/// Gets or sets Device ID of used audio render device.
119+
/// </summary>
120+
string audioRenderDeviceId;
111121

112122
/// <summary>
113123
/// Gets or sets if audio post-capture and pre-render events should be

windows/wrapper/impl_org_webRtc_WebRtcFactory.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ void WrapperImplType::setup() noexcept
326326

327327
bool audioCapturingEnabled = configuration_ ? configuration_->audioCapturingEnabled : true;
328328
bool audioRenderingEnabled = configuration_ ? configuration_->audioRenderingEnabled : true;
329+
String audioCaptureDeviceId = configuration_ ? configuration_->audioCaptureDeviceId : String();
330+
String audioRenderDeviceId = configuration_ ? configuration_->audioRenderDeviceId : String();
329331
bool enableAudioProcessingEvents = configuration_ ? configuration_->enableAudioBufferEvents : false;
330332

331333
networkThread = rtc::Thread::CreateWithSocketServer();
@@ -351,6 +353,38 @@ void WrapperImplType::setup() noexcept
351353
return rtc::scoped_refptr<::webrtc::AudioDeviceModule>(webrtc::IAudioDeviceWasapi::create(props));
352354
});
353355

356+
if (audioCaptureDeviceId.size() != 0) {
357+
int deviceCount = audioDeviceModule->RecordingDevices();
358+
char deviceName[256];
359+
char deviceId[256];
360+
uint16_t deviceIndex = USHRT_MAX;
361+
for (uint16_t i = 0; i < deviceCount; i++) {
362+
audioDeviceModule->RecordingDeviceName(i, deviceName, deviceId);
363+
if (strcmp(audioCaptureDeviceId.c_str(), deviceId) == 0) {
364+
deviceIndex = i;
365+
break;
366+
}
367+
}
368+
if (deviceIndex != USHRT_MAX)
369+
audioDeviceModule->SetRecordingDevice(deviceIndex);
370+
}
371+
372+
if (audioRenderDeviceId.size() != 0) {
373+
int deviceCount = audioDeviceModule->PlayoutDevices();
374+
char deviceName[256];
375+
char deviceId[256];
376+
uint16_t deviceIndex = USHRT_MAX;
377+
for (uint16_t i = 0; i < deviceCount; i++) {
378+
audioDeviceModule->PlayoutDeviceName(i, deviceName, deviceId);
379+
if (strcmp(audioRenderDeviceId.c_str(), deviceId) == 0) {
380+
deviceIndex = i;
381+
break;
382+
}
383+
}
384+
if (deviceIndex != USHRT_MAX)
385+
audioDeviceModule->SetPlayoutDevice(deviceIndex);
386+
}
387+
354388
rtc::scoped_refptr<::webrtc::AudioProcessing> audioProcessing;
355389
if (enableAudioProcessingEvents)
356390
audioProcessing = rtc::scoped_refptr<::webrtc::AudioProcessing>{::webrtc::AudioProcessingBuilder().SetCapturePostProcessing(std::move(audioPostCaptureInit_)).SetRenderPreProcessing(std::move(audioPreRenderInit_)).Create() };

windows/wrapper/impl_org_webRtc_WebRtcFactoryConfiguration.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ WrapperImplTypePtr WrapperImplType::clone(WrapperTypePtr wrapper) noexcept
6161
auto result = std::make_shared<WrapperImplType>();
6262
result->audioCapturingEnabled = converted->audioCapturingEnabled;
6363
result->audioRenderingEnabled = converted->audioRenderingEnabled;
64+
result->audioCaptureDeviceId = converted->audioCaptureDeviceId;
65+
result->audioRenderDeviceId = converted->audioRenderDeviceId;
6466
result->enableAudioBufferEvents = converted->enableAudioBufferEvents;
6567
return result;
6668
}

windows/wrapper/impl_webrtc_AudioDeviceWasapi.cpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,21 +1865,13 @@ namespace webrtc
18651865
// Refresh the list of rendering endpoint devices
18661866
RefreshDeviceList(DeviceClass::AudioRender);
18671867

1868-
if (initialized_) {
1869-
return (DeviceListCount(DeviceClass::AudioRender));
1870-
}
1871-
1872-
return -1;
1868+
return (DeviceListCount(DeviceClass::AudioRender));
18731869
}
18741870

18751871
// ----------------------------------------------------------------------------
18761872
// SetPlayoutDevice I (II)
18771873
// ----------------------------------------------------------------------------
18781874
int32_t AudioDeviceWasapi::SetPlayoutDevice(uint16_t index) {
1879-
if (playIsInitialized_) {
1880-
return -1;
1881-
}
1882-
18831875
if (!playoutEnabled_) {
18841876
return 0;
18851877
}
@@ -1922,11 +1914,13 @@ namespace webrtc
19221914
// ----------------------------------------------------------------------------
19231915
int32_t AudioDeviceWasapi::SetPlayoutDevice(
19241916
AudioDeviceModule::WindowsDeviceType device) {
1925-
if (playIsInitialized_) {
1926-
return -1;
1917+
if (!playoutEnabled_) {
1918+
return 0;
19271919
}
19281920

1929-
if (!playoutEnabled_) {
1921+
// If device has been selected once by index value it couldn't be changed afterwards
1922+
// by setting the device type
1923+
if (usingOutputDeviceIndex_) {
19301924
return 0;
19311925
}
19321926

@@ -1955,8 +1949,6 @@ namespace webrtc
19551949
str.c_str());
19561950
}
19571951

1958-
usingOutputDeviceIndex_ = false;
1959-
19601952
return 0;
19611953
}
19621954

@@ -2120,21 +2112,13 @@ namespace webrtc
21202112
// Refresh the list of capture endpoint devices
21212113
RefreshDeviceList(DeviceClass::AudioCapture);
21222114

2123-
if (initialized_) {
2124-
return (DeviceListCount(DeviceClass::AudioCapture));
2125-
}
2126-
2127-
return -1;
2115+
return (DeviceListCount(DeviceClass::AudioCapture));
21282116
}
21292117

21302118
// ----------------------------------------------------------------------------
21312119
// SetRecordingDevice I (II)
21322120
// ----------------------------------------------------------------------------
21332121
int32_t AudioDeviceWasapi::SetRecordingDevice(uint16_t index) {
2134-
if (recIsInitialized_) {
2135-
return -1;
2136-
}
2137-
21382122
if (!recordingEnabled_) {
21392123
return 0;
21402124
}
@@ -2178,11 +2162,13 @@ namespace webrtc
21782162
// ----------------------------------------------------------------------------
21792163
int32_t AudioDeviceWasapi::SetRecordingDevice(
21802164
AudioDeviceModule::WindowsDeviceType device) {
2181-
if (recIsInitialized_) {
2182-
return -1;
2165+
if (!recordingEnabled_) {
2166+
return 0;
21832167
}
21842168

2185-
if (!recordingEnabled_) {
2169+
// If device has been selected once by index value it couldn't be changed
2170+
// afterwards by setting the device type
2171+
if (usingInputDeviceIndex_) {
21862172
return 0;
21872173
}
21882174

@@ -2212,8 +2198,6 @@ namespace webrtc
22122198
GetDeviceName(captureDevice_));
22132199
}
22142200

2215-
usingInputDeviceIndex_ = false;
2216-
22172201
return 0;
22182202
}
22192203

0 commit comments

Comments
 (0)