@@ -374,8 +374,6 @@ void LLWebRTCImpl::terminate()
374374
375375 mSignalingThread ->BlockingCall ([this ]() { mPeerConnectionFactory = nullptr ; });
376376
377- mPeerConnections .clear ();
378-
379377 mWorkerThread ->BlockingCall (
380378 [this ]()
381379 {
@@ -386,6 +384,14 @@ void LLWebRTCImpl::terminate()
386384 mDeviceModule = nullptr ;
387385 mTaskQueueFactory = nullptr ;
388386 });
387+
388+ // In case peer connections still somehow have jobs in workers,
389+ // only clear connections up after clearing workers.
390+ mNetworkThread = nullptr ;
391+ mWorkerThread = nullptr ;
392+ mSignalingThread = nullptr ;
393+
394+ mPeerConnections .clear ();
389395 webrtc::LogMessage::RemoveLogToStream (mLogSink );
390396}
391397
@@ -766,6 +772,7 @@ void LLWebRTCImpl::freePeerConnection(LLWebRTCPeerConnectionInterface* peer_conn
766772 std::find (mPeerConnections .begin (), mPeerConnections .end (), peer_connection);
767773 if (it != mPeerConnections .end ())
768774 {
775+ // Todo: make sure conection had no jobs in workers
769776 mPeerConnections .erase (it);
770777 if (mPeerConnections .empty ())
771778 {
@@ -785,14 +792,19 @@ LLWebRTCPeerConnectionImpl::LLWebRTCPeerConnectionImpl() :
785792 mWebRTCImpl (nullptr ),
786793 mPeerConnection (nullptr ),
787794 mMute (MUTE_INITIAL),
788- mAnswerReceived (false )
795+ mAnswerReceived (false ),
796+ mPendingJobs (0 )
789797{
790798}
791799
792800LLWebRTCPeerConnectionImpl::~LLWebRTCPeerConnectionImpl ()
793801{
794802 mSignalingObserverList .clear ();
795803 mDataObserverList .clear ();
804+ if (mPendingJobs > 0 )
805+ {
806+ RTC_LOG (LS_ERROR) << __FUNCTION__ << " Destroying a connection that has " << std::to_string (mPendingJobs ) << " unfinished jobs that might cause workers to crash" ;
807+ }
796808}
797809
798810//
@@ -804,8 +816,10 @@ void LLWebRTCPeerConnectionImpl::init(LLWebRTCImpl * webrtc_impl)
804816 mWebRTCImpl = webrtc_impl;
805817 mPeerConnectionFactory = mWebRTCImpl ->getPeerConnectionFactory ();
806818}
819+
807820void LLWebRTCPeerConnectionImpl::terminate ()
808821{
822+ mPendingJobs ++;
809823 mWebRTCImpl ->PostSignalingTask (
810824 [this ]()
811825 {
@@ -848,7 +862,9 @@ void LLWebRTCPeerConnectionImpl::terminate()
848862 observer->OnPeerConnectionClosed ();
849863 }
850864 }
865+ mPendingJobs --;
851866 });
867+ mPeerConnectionFactory .release ();
852868}
853869
854870void LLWebRTCPeerConnectionImpl::setSignalingObserver (LLWebRTCSignalingObserver *observer) { mSignalingObserverList .emplace_back (observer); }
@@ -869,6 +885,7 @@ bool LLWebRTCPeerConnectionImpl::initializeConnection(const LLWebRTCPeerConnecti
869885 RTC_DCHECK (!mPeerConnection );
870886 mAnswerReceived = false ;
871887
888+ mPendingJobs ++;
872889 mWebRTCImpl ->PostSignalingTask (
873890 [this ,options]()
874891 {
@@ -902,6 +919,7 @@ bool LLWebRTCPeerConnectionImpl::initializeConnection(const LLWebRTCPeerConnecti
902919 {
903920 observer->OnRenegotiationNeeded ();
904921 }
922+ mPendingJobs --;
905923 return ;
906924 }
907925
@@ -964,6 +982,7 @@ bool LLWebRTCPeerConnectionImpl::initializeConnection(const LLWebRTCPeerConnecti
964982
965983 webrtc::PeerConnectionInterface::RTCOfferAnswerOptions offerOptions;
966984 mPeerConnection ->CreateOffer (this , offerOptions);
985+ mPendingJobs --;
967986 });
968987
969988 return true ;
@@ -1006,6 +1025,7 @@ void LLWebRTCPeerConnectionImpl::AnswerAvailable(const std::string &sdp)
10061025{
10071026 RTC_LOG (LS_INFO) << __FUNCTION__ << " Remote SDP: " << sdp;
10081027
1028+ mPendingJobs ++;
10091029 mWebRTCImpl ->PostSignalingTask (
10101030 [this , sdp]()
10111031 {
@@ -1015,6 +1035,7 @@ void LLWebRTCPeerConnectionImpl::AnswerAvailable(const std::string &sdp)
10151035 mPeerConnection ->SetRemoteDescription (webrtc::CreateSessionDescription (webrtc::SdpType::kAnswer , sdp),
10161036 webrtc::scoped_refptr<webrtc::SetRemoteDescriptionObserverInterface>(this ));
10171037 }
1038+ mPendingJobs --;
10181039 });
10191040}
10201041
@@ -1037,6 +1058,7 @@ void LLWebRTCPeerConnectionImpl::setMute(bool mute)
10371058 mMute = new_state;
10381059
10391060
1061+ mPendingJobs ++;
10401062 mWebRTCImpl ->PostSignalingTask (
10411063 [this , force_reset, enable]()
10421064 {
@@ -1060,6 +1082,7 @@ void LLWebRTCPeerConnectionImpl::setMute(bool mute)
10601082 track->set_enabled (enable);
10611083 }
10621084 }
1085+ mPendingJobs --;
10631086 }
10641087 });
10651088}
@@ -1081,6 +1104,7 @@ void LLWebRTCPeerConnectionImpl::resetMute()
10811104
10821105void LLWebRTCPeerConnectionImpl::setReceiveVolume (float volume)
10831106{
1107+ mPendingJobs ++;
10841108 mWebRTCImpl ->PostSignalingTask (
10851109 [this , volume]()
10861110 {
@@ -1099,11 +1123,13 @@ void LLWebRTCPeerConnectionImpl::setReceiveVolume(float volume)
10991123 }
11001124 }
11011125 }
1126+ mPendingJobs --;
11021127 });
11031128}
11041129
11051130void LLWebRTCPeerConnectionImpl::setSendVolume (float volume)
11061131{
1132+ mPendingJobs ++;
11071133 mWebRTCImpl ->PostSignalingTask (
11081134 [this , volume]()
11091135 {
@@ -1114,6 +1140,7 @@ void LLWebRTCPeerConnectionImpl::setSendVolume(float volume)
11141140 track->GetSource ()->SetVolume (volume*5.0 );
11151141 }
11161142 }
1143+ mPendingJobs --;
11171144 });
11181145}
11191146
@@ -1190,11 +1217,13 @@ void LLWebRTCPeerConnectionImpl::OnConnectionChange(webrtc::PeerConnectionInterf
11901217 {
11911218 case webrtc::PeerConnectionInterface::PeerConnectionState::kConnected :
11921219 {
1220+ mPendingJobs ++;
11931221 mWebRTCImpl ->PostWorkerTask ([this ]() {
11941222 for (auto &observer : mSignalingObserverList )
11951223 {
11961224 observer->OnAudioEstablished (this );
11971225 }
1226+ mPendingJobs --;
11981227 });
11991228 break ;
12001229 }
@@ -1452,11 +1481,13 @@ void LLWebRTCPeerConnectionImpl::sendData(const std::string& data, bool binary)
14521481 {
14531482 webrtc::CopyOnWriteBuffer cowBuffer (data.data (), data.length ());
14541483 webrtc::DataBuffer buffer (cowBuffer, binary);
1484+ mPendingJobs ++;
14551485 mWebRTCImpl ->PostNetworkTask ([this , buffer]() {
14561486 if (mDataChannel )
14571487 {
14581488 mDataChannel ->Send (buffer);
14591489 }
1490+ mPendingJobs --;
14601491 });
14611492 }
14621493}
0 commit comments