我们先来看看 WebRTC 中提供音频设备输入输出功能的类结构:

从上图中我们看到:

  • AudioDeviceModule接口类是音频设备模型的基类,该接口类及其子类并不提供任何针对音频的实际操作,实际操作都是由AudioDeviceGeneric接口类的子类来提供。

  • AudioDeviceGeneric接口类是音频设备实际操作(如录音、播放、声音控制等)的基类,然后根据平台不同由不同的类来实现具体操作,如 windows 平台由AudioDeviceWindowsCore来负责实现。

通过代码追踪,我们可以发现 WebRTC 默认使用的是计算机上的系统默认音频输入输出设备,以及默认的视频采集设备,这些我们通过adm_helpers.cc::Init函数来验证(笔者这里使用的是m77分支):

1
2
3
4
5
6
7
8
9
if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) {
RTC_LOG(LS_ERROR) << "Unable to set playout device.";
return;
}

if (adm->SetRecordingDevice(AUDIO_DEVICE_ID) != 0) {
RTC_LOG(LS_ERROR) << "Unable to set recording device.";
return;
}

所以即便我们调用了SetPlayoutDeviceSetRecordingDevice也无济于事,因为在此处仍然会被覆盖为使用默认设备。
因此我们只需要针对此处进行修改即可,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
uint16_t playout_index = 0;
// 先获取设备index,如果能够获取到,表示外部已经设备设备index,放弃使用默认设备
// AudioDeviceModule没有提供Get接口,需要自己添加
//
if (adm->GetPlayoutDevice(&playout_index) == 0) {
RTC_LOG(LS_INFO) << "Playout device index: " << playout_index;
} else {
if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) {
RTC_LOG(LS_ERROR) << "Unable to set playout device.";
return;
}
}

uint16_t recording_index = 0;
if (adm->GetRecordingDevice(&recording_index) == 0) {
RTC_LOG(LS_INFO) << "Recording device index: " << recording_index;
} else {
if (adm->SetRecordingDevice(AUDIO_DEVICE_ID) != 0) {
RTC_LOG(LS_ERROR) << "Unable to set recording device.";
return;
}
}

细心的读者可能发现,AudioDeviceModule接口类只定义了 Playout/Recoding 设备的Set接口,但没有提供Get接口,因为 WebRTC 默认使用的是系统设备,所以外界不需要知道具体设备 index,故没有提供(笔者使用的是 webrtc m77分支,也许 webrtc 的后续版本会加入该接口)。我们自己也可以很容易的在 AudioDeviceModule 中加入Get接口。

上面的一切修改完毕之后,我们只需要定义一个 adm,作为参数传入到webrtc::CreatePeerConnectionFactory函数即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
m_adm = webrtc::AudioDeviceModule::Create(
webrtc::AudioDeviceModule::kPlatformDefaultAudio, webrtc::CreateDefaultTaskQueueFactory().get());

int32_t ret = m_adm->Init();
Q_ASSERT(ret == 0);

if (m_connParameter.audioInputDeviceIndex == -1) {
m_adm->SetEnableRecording(false);
} else {
ret = m_adm->SetRecordingDevice(m_connParameter.audioInputDeviceIndex);
Q_ASSERT(ret == 0);
}

if (m_connParameter.audioOutputDeviceIndex == -1) {
m_adm->SetEnablePlayout(false);
} else {
ret = m_adm->SetPlayoutDevice(m_connParameter.audioOutputDeviceIndex);
Q_ASSERT(ret == 0);
}