webrtc捕获多个同一类摄像头

如果有多个同款摄像头,先查看摄像头信息,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一起来当作摄像头的标识了,也就解决了区分多个同类摄像头问题。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_34214088

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值