如果有多个同款摄像头,先查看摄像头信息,GetDeviceName得到摄像头name,id,然后为每个摄像头创建一个捕获对象capturer = factory.Create(cricket::Device(sName, sId)); 但是这里创建capturer时,用的是name区别每个摄像头,因此会导致name相同的摄像头无法区分开来
std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(webrtc::VideoCaptureFactory::CreateDeviceInfo());
int cameraNum = info->NumberOfDevices();
for (int i = 0; i < cameraNum; ++i) {
const uint32_t kSize = 256;
char sName[kSize] = {0};
char sId[kSize] = {0};
if (info->GetDeviceName(0, sName, kSize, sId, kSize) != -1) {
cricket::WebRtcVideoDeviceCapturerFactory factory;
std::unique_ptr<cricket::VideoCapturer> capturer = factory.Create(cricket::Device(sName, sId));
}
}
查看webrtc源码,创建capturer,media/engine/webrtcvideocapturerfactory.cc中的Create函数
std::unique_ptr<VideoCapturer> WebRtcVideoDeviceCapturerFactory::Create(
const Device& device) {
#ifdef HAVE_WEBRTC_VIDEO
std::unique_ptr<WebRtcVideoCapturer> capturer(new WebRtcVideoCapturer());
if (!capturer->Init(device)) {
return std::unique_ptr<VideoCapturer>();
}
return std::move(capturer);
#else
return std::unique_ptr<VideoCapturer>();
#endif
}
从上面可以看出创建了一个WebRtcVideoCapturer实例以后再将它初始化,初始化方法在media/engine/webrtcvideocapturer.cc中
bool WebRtcVideoCapturer::Init(const Device& device) {
RTC_DCHECK(!start_thread_);
if (module_) {
RTC_LOG(LS_ERROR) << "The capturer is already initialized";
return false;
}
webrtc::VideoCaptureModule::DeviceInfo* info = factory_->CreateDeviceInfo();
if (!info) {
return false;
}
//这里提到了源码用的是name来匹配摄像头
// Find the desired camera, by name.
// In the future, comparing IDs will be more robust.
// TODO(juberti): Figure what's needed to allow this.
int num_cams = info->NumberOfDevices();
char vcm_id[256] = "";
bool found = false;
for (int index = 0; index < num_cams; ++index) {
char vcm_name[256];
if (info->GetDeviceName(index, vcm_name, arraysize(vcm_name), vcm_id,
arraysize(vcm_id)) != -1) {
/**************[SPACECRAFT]***********/
//if (device.name == reinterpret_cast<char*>(vcm_name)) { //这是源码,使用name来匹配摄像头
if (device.name == reinterpret_cast<char*>(vcm_name) && device.id == reinterpret_cast<char*>(vcm_id)) {//这是我做的修改,使用name和id一起来匹配摄像头
/**************[SPACECRAFT]***********/
found = true;
break;
}
}
}
...
}
如上面修改了Init方法后重新编译源码,使修改生效
$ ninja -C ../Debug
修改完以后,就可以使用name和id一起来当作摄像头的标识了,也就解决了区分多个同类摄像头问题。