前言



源码分析
自己的是20210315的源码,跟老师的版本不一样。
在client单击连接对端的时候,最后是调用MaybeStartThread函数来创建线程的。如果传入自己的线程指针则直接返回,否则创建Thread对象。
Conductor::ConnectToPeer->Conductor::InitializePeerConnection->webrtc::CreatePeerConnectionFactory->CreateModularPeerConnectionFactory(***auto pc_factory = PeerConnectionFactory::Create(std::move(dependencies));**->rtc::scoped_refptr<PeerConnectionFactory> PeerConnectionFactory::Create(PeerConnectionFactoryDependencies dependencies) {auto context = ConnectionContext::Create(&dependencies);if (!context) {return nullptr;}return new rtc::RefCountedObject<PeerConnectionFactory>(context,&dependencies);}->rtc::scoped_refptr<ConnectionContext> ConnectionContext::Create(PeerConnectionFactoryDependencies* dependencies) {auto context = new rtc::RefCountedObject<ConnectionContext>(dependencies);if (!context->channel_manager_->Init()) {return nullptr;}return context;}->ConnectionContext::ConnectionContext->MaybeStartThread
MaybeStartThread
rtc::Thread* MaybeStartThread(rtc::Thread* old_thread,const std::string& thread_name,bool with_socket_server,std::unique_ptr<rtc::Thread>& thread_holder) {if (old_thread) {return old_thread;}if (with_socket_server) {thread_holder = rtc::Thread::CreateWithSocketServer();} else {thread_holder = rtc::Thread::Create();}thread_holder->SetName(thread_name, nullptr);thread_holder->Start();return thread_holder.get();}
在里面的Thread::Start函数里面创建了线程,Windows则调用CreateThread,Linux调用pthread_create
Thread::Start
bool Thread::Start() {RTC_DCHECK(!IsRunning());if (IsRunning())return false;Restart(); // reset IsQuitting() if the thread is being restarted// Make sure that ThreadManager is created on the main thread before// we start a new thread.ThreadManager::Instance();owned_ = true;#if defined(WEBRTC_WIN)thread_ = CreateThread(nullptr, 0, PreRun, this, 0, &thread_id_);if (!thread_) {return false;}#elif defined(WEBRTC_POSIX)pthread_attr_t attr;pthread_attr_init(&attr);int error_code = pthread_create(&thread_, &attr, PreRun, this);if (0 != error_code) {RTC_LOG(LS_ERROR) << "Unable to create pthread, error " << error_code;thread_ = 0;return false;}RTC_DCHECK(thread_);#endifreturn true;}
这里主要是说  CreateThread(nullptr, 0, PreRun, this, 0, &threadid);
线程创建完,则会调用PreRun
Thread::PreRun
#if defined(WEBRTC_WIN)DWORD WINAPI Thread::PreRun(LPVOID pv) {#elsevoid* Thread::PreRun(void* pv) {#endifThread* thread = static_cast<Thread*>(pv);ThreadManager::Instance()->SetCurrentThread(thread);rtc::SetCurrentThreadName(thread->name_.c_str());#if defined(WEBRTC_MAC)ScopedAutoReleasePool pool;#endifthread->Run();ThreadManager::Instance()->SetCurrentThread(nullptr);#ifdef WEBRTC_WINreturn 0;#elsereturn nullptr;#endif} // namespace rtc
Thread::Run
void Thread::Run() {ProcessMessages(kForever);}-》 主要是 Dispatch(&msg);bool Thread::ProcessMessages(int cmsLoop) {// Using ProcessMessages with a custom clock for testing and a time greater// than 0 doesn't work, since it's not guaranteed to advance the custom// clock's time, and may get stuck in an infinite loop.RTC_DCHECK(GetClockForTesting() == nullptr || cmsLoop == 0 ||cmsLoop == kForever);int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop);int cmsNext = cmsLoop;while (true) {#if defined(WEBRTC_MAC)ScopedAutoReleasePool pool;#endifMessage msg;if (!Get(&msg, cmsNext))return !IsQuitting();Dispatch(&msg);if (cmsLoop != kForever) {cmsNext = static_cast<int>(TimeUntil(msEnd));if (cmsNext < 0)return true;}}}-》 主要是 pmsg->phandler->OnMessagevoid Thread::Dispatch(Message* pmsg) {TRACE_EVENT2("webrtc", "Thread::Dispatch", "src_file",pmsg->posted_from.file_name(), "src_func",pmsg->posted_from.function_name());RTC_DCHECK_RUN_ON(this);int64_t start_time = TimeMillis();pmsg->phandler->OnMessage(pmsg);int64_t end_time = TimeMillis();int64_t diff = TimeDiff(end_time, start_time);if (diff >= dispatch_warning_ms_) {RTC_LOG(LS_INFO) << "Message to " << name() << " took " << diff<< "ms to dispatch. Posted from: "<< pmsg->posted_from.ToString();// To avoid log spew, move the warning limit to only give warning// for delays that are larger than the one observed.dispatch_warning_ms_ = diff + 1;}}
学习资料
WebRTC源码分析-线程基础之线程基本功能
https://www.jianshu.com/p/32fcf61c48e6
WebRTC源码分析-线程基础之消息循环,消息投递
https://www.jianshu.com/p/a3547ffefbeb
