WebRTC中的三个主要线程:
- signaling_thread,信号线程:用于与应用层交互
- worker_thread,工作线程(最核心):负责内部逻辑处理
- network_thread,网络线程:负责网络数据包的收发
webrtc中的其他线程都是通过这三个线程创建出来的!!
webrtc中创建三个线程的位置:
// src/examples/peerconnection/client/conductor.cc
bool Conductor::InitializePeerConnection() {peer_connection_factory_ = webrtc::CreatePeerConnectionFactory();
}// src/pc/peer_connection_factory.cc
PeerConnectionFactory::PeerConnectionFactory(PeerConnectionFactoryDependencies dependencies)
{if (!network_thread_) {owned_network_thread_ = rtc::Thread::CreateWithSocketServer(); // CreateWithSocketServer() 用于处理网络事件owned_network_thread_->SetName("pc_network_thread", nullptr);owned_network_thread_->Start();network_thread_ = owned_network_thread_.get();}if (!worker_thread_) {owned_worker_thread_ = rtc::Thread::Create(); // Create() 不处理网络事件owned_worker_thread_->SetName("pc_worker_thread", nullptr);owned_worker_thread_->Start();worker_thread_ = owned_worker_thread_.get();}if (!signaling_thread_) {signaling_thread_ = rtc::Thread::Current(); // Current() 默认将主线程当做signal线程if (!signaling_thread_) {signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread();wraps_current_thread_ = true;}}
}
PS:编码技巧:当传入的参数较多时,可以打包成一个结构体传下去,如dependencies,其中指定
dependencies.network_thread, dependencies.signaling_thread, dependencies.worker_thread 等,
以及火山引擎SDK中的video_frame等,都是如此