diff --git a/include/Handler.hpp b/include/Handler.hpp index 11151323..41b8d63b 100644 --- a/include/Handler.hpp +++ b/include/Handler.hpp @@ -59,6 +59,7 @@ namespace mediasoupclient /* Methods inherited from PeerConnectionListener. */ public: + void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState newState) override; void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState newState) override; protected: diff --git a/include/PeerConnection.hpp b/include/PeerConnection.hpp index 93b81470..c2884729 100644 --- a/include/PeerConnection.hpp +++ b/include/PeerConnection.hpp @@ -112,6 +112,7 @@ namespace mediasoupclient ~PeerConnection() = default; void Close(); + void CloseThreads(); webrtc::PeerConnectionInterface::RTCConfiguration GetConfiguration() const; bool SetConfiguration(const webrtc::PeerConnectionInterface::RTCConfiguration& config); std::string CreateOffer(const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options); @@ -135,9 +136,9 @@ namespace mediasoupclient private: // Signaling and worker threads. - std::unique_ptr networkThread; - std::unique_ptr signalingThread; - std::unique_ptr workerThread; + std::unique_ptr networkThread{ nullptr }; + std::unique_ptr signalingThread{ nullptr }; + std::unique_ptr workerThread{ nullptr }; // PeerConnection factory. rtc::scoped_refptr peerConnectionFactory; diff --git a/src/Handler.cpp b/src/Handler.cpp index 9e6a9547..bd66f7f9 100644 --- a/src/Handler.cpp +++ b/src/Handler.cpp @@ -110,6 +110,14 @@ namespace mediasoupclient MSC_THROW_ERROR("failed to update ICE servers"); }; + void Handler::OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState newState) + { + MSC_TRACE(); + + if (newState == webrtc::PeerConnectionInterface::SignalingState::kClosed) + this->pc->CloseThreads(); + } + void Handler::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState newState) { MSC_TRACE(); diff --git a/src/PeerConnection.cpp b/src/PeerConnection.cpp index 849db1fc..bb3c1c02 100644 --- a/src/PeerConnection.cpp +++ b/src/PeerConnection.cpp @@ -118,6 +118,20 @@ namespace mediasoupclient this->pc->Close(); } + void PeerConnection::CloseThreads() + { + MSC_TRACE(); + + if (this->networkThread != nullptr) + this->networkThread->Quit(); + + if (this->signalingThread != nullptr) + this->signalingThread->Quit(); + + if (this->workerThread != nullptr) + this->workerThread->Quit(); + } + webrtc::PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() const { MSC_TRACE();