WebRTC--三大线程

一  实现

//Thread从MessageQueue派生,有消息队列
class LOCKABLE Thread : public MessageQueue {}
rtc::Thread* network_thread_;

//Convenience method to invoke a functor on another thread
1 xxx_n:network线程执行的
std::unique_ptr<SessionStats> WebRtcSession::GetStats(
    const ChannelNamePairs& channel_name_pairs) {
  if (network_thread()->IsCurrent()) {
    return GetStats_n(channel_name_pairs);
  }
  return network_thread()->Invoke<std::unique_ptr<SessionStats>>(
      RTC_FROM_HERE,
      rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs));
}

//把消息放到队列:MessageQueue::Post(
2 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS,
                           new GetStatsMsg(observer, track));

//ThreadManager统一管理
signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread();

二  三大线程在那创建

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 |network_thread| or |worker_thread| are null, the PeerConnectionFactory
// will create the necessary thread internally. If |signaling_thread| is null,
// the PeerConnectionFactory will use the thread on which this method is called
// as the signaling thread, wrapping it in an rtc::Thread object if needed.
RTC_EXPORT rtc::scoped_refptr<PeerConnectionFactoryInterface>
CreateModularPeerConnectionFactory(
    PeerConnectionFactoryDependencies dependencies);

}

src\pc\peer_connection_factory.cc
rtc::scoped_refptr<PeerConnectionFactoryInterface>
CreateModularPeerConnectionFactory(
{
// The PeerConnectionFactory must be created on the signaling thread.

auto pc_factory = PeerConnectionFactory::Create(std::move(dependencies));

//PeerConnectionFactory::Create  实现
PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(),
                                            pc_factory);
}

//用宏扩展实现Proxy
static rtc::scoped_refptr<c##ProxyWithInternal> Create(                      \
      rtc::Thread* primary_thread, INTERNAL_CLASS* c) {                        \
    return new rtc::RefCountedObject<c##ProxyWithInternal>(primary_thread, c); \
  }

rtc::scoped_refptr<PeerConnectionFactory> PeerConnectionFactory::Create(
    PeerConnectionFactoryDependencies dependencies) {
  auto context = ConnectionContext::Create(&dependencies);
  if (!context) {
    return nullptr;
  }
  return new rtc::RefCountedObject<PeerConnectionFactory>(context,
                                                          &dependencies);
}

 三 在实际项目中用用

std::unique_ptr<rtc::Thread>  worker_thread_ = nullptr;
worker_thread_  = rtc::Thread::Create();
worker_thread_->SetName("AudioWorkerThread", this);
worker_thread_->Start();

//是不是AudioDeviceModule::Create、等要求在一个线程中执行???
int32_t  RealAudioImpl::Init() {
	printf("%s  id=%d\n", __FUNCTION__, GetCurrentThreadId());
	if (worker_thread_) {
		return worker_thread_->Invoke<int32_t>(RTC_FROM_HERE, 
           rtc::Bind(&TALAudioImpl::DoInit, this));
	}
	return  -1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值