解决webrtc封装sdk时正常调用creatoffer不回调的问题

在使用webrtc peerconnection_client封装sdk时遇到了createoffer调用成功后不回调的问题。现将问题记录下来,同时希望可以帮助遇到此问题的同学。
该问题是由于signaling_thread的消息循环引起的。
在conductor.cc中创建peer_connection_factory_ 时默认指定signaling_thread为nullptr,这种情况下将conductor封装时会出现createoffer成功但是没有触发回调的问题

bool Conductor::InitializePeerConnection() {
  RTC_DCHECK(!peer_connection_factory_);
  RTC_DCHECK(!peer_connection_);

  peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
      nullptr /* network_thread */, nullptr /* worker_thread */,
      nullptr /* signaling_thread */, nullptr /* default_adm */,
      webrtc::CreateBuiltinAudioEncoderFactory(),
      webrtc::CreateBuiltinAudioDecoderFactory(),
      webrtc::CreateBuiltinVideoEncoderFactory(),
      webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
      nullptr /* audio_processing */);

  if (!peer_connection_factory_) {
    main_wnd_->MessageBox("Error", "Failed to initialize PeerConnectionFactory",
                          true);
    DeletePeerConnection();
    return false;
  }

  if (!CreatePeerConnection(/*dtls=*/true)) {
    main_wnd_->MessageBox("Error", "CreatePeerConnection failed", true);
    DeletePeerConnection();
  }

  AddTracks();

  return peer_connection_ != nullptr;
}

解决这个问题需要手动指定signaling_thread,并且指定当前的线程,network_thread和worker_thread不影响此问题。
代码如下:

rtc::ThreadManager::Instance()->WrapCurrentThread();

 network_thread_ = rtc::Thread::CreateWithSocketServer();
 network_thread_->SetName("network_thread", nullptr);
 network_thread_->Start();

 worker_thread_ = rtc::Thread::Create();
 worker_thread_->SetName("worker_thread", nullptr);
 worker_thread_->Start();

 signaling_thread_ = rtc::Thread::Create();
 signaling_thread_->SetName("signaling_thread", nullptr);
 signaling_thread_->Start();

 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
   network_thread_.get() /* network_thread */, worker_thread_.get() /* worker_thread */,
   signaling_thread_.get() /* signaling_thread */, nullptr /* default_adm */,
   webrtc::CreateBuiltinAudioEncoderFactory(),
   webrtc::CreateBuiltinAudioDecoderFactory(),
   webrtc::CreateBuiltinVideoEncoderFactory(),
   webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
   nullptr /* audio_processing */);

你可能感兴趣的:(解决webrtc封装sdk时正常调用creatoffer不回调的问题)