Skip to content

Commit

Permalink
Closing threads
Browse files Browse the repository at this point in the history
When creating a new PeerConnection without a PeerConnectionFactory, network, signaling and worker threads are created.
After closing the peer connection, Handler will close those threads if they exist after receiving the OnSignalingChange for "close" state.
  • Loading branch information
pablito25sp committed Jun 9, 2021
1 parent 58c5c66 commit d48a07d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/Handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions include/PeerConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -135,9 +136,9 @@ namespace mediasoupclient

private:
// Signaling and worker threads.
std::unique_ptr<rtc::Thread> networkThread;
std::unique_ptr<rtc::Thread> signalingThread;
std::unique_ptr<rtc::Thread> workerThread;
std::unique_ptr<rtc::Thread> networkThread{ nullptr };
std::unique_ptr<rtc::Thread> signalingThread{ nullptr };
std::unique_ptr<rtc::Thread> workerThread{ nullptr };

// PeerConnection factory.
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peerConnectionFactory;
Expand Down
8 changes: 8 additions & 0 deletions src/Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/PeerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d48a07d

Please sign in to comment.