Parcourir la source

Introduced signaling mutex on PeerConnection interface

Paul-Louis Ageneau il y a 3 ans
Parent
commit
f39d7b3cc5
3 fichiers modifiés avec 9 ajouts et 2 suppressions
  1. 0 1
      include/rtc/peerconnection.hpp
  2. 1 0
      src/impl/peerconnection.hpp
  3. 8 1
      src/peerconnection.cpp

+ 0 - 1
include/rtc/peerconnection.hpp

@@ -89,7 +89,6 @@ public:
 	bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
 
 	void setLocalDescription(Description::Type type = Description::Type::Unspec);
-
 	void setRemoteDescription(Description description);
 	void addRemoteCandidate(Candidate candidate);
 

+ 1 - 0
src/impl/peerconnection.hpp

@@ -106,6 +106,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
 	std::atomic<GatheringState> gatheringState = GatheringState::New;
 	std::atomic<SignalingState> signalingState = SignalingState::Stable;
 	std::atomic<bool> negotiationNeeded = false;
+	std::mutex signalingMutex;
 
 	synchronized_callback<shared_ptr<rtc::DataChannel>> dataChannelCallback;
 	synchronized_callback<Description> localDescriptionCallback;

+ 8 - 1
src/peerconnection.cpp

@@ -78,6 +78,7 @@ bool PeerConnection::hasMedia() const {
 }
 
 void PeerConnection::setLocalDescription(Description::Type type) {
+	std::unique_lock signalingLock(impl()->signalingMutex);
 	PLOG_VERBOSE << "Setting local description, type=" << Description::typeToString(type);
 
 	SignalingState signalingState = impl()->signalingState.load();
@@ -144,6 +145,7 @@ void PeerConnection::setLocalDescription(Description::Type type) {
 	impl()->processLocalDescription(std::move(local));
 
 	impl()->changeSignalingState(newSignalingState);
+	signalingLock.unlock();
 
 	if (impl()->gatheringState == GatheringState::New) {
 		iceTransport->gatherLocalCandidates(impl()->localBundleMid());
@@ -151,6 +153,7 @@ void PeerConnection::setLocalDescription(Description::Type type) {
 }
 
 void PeerConnection::setRemoteDescription(Description description) {
+	std::unique_lock signalingLock(impl()->signalingMutex);
 	PLOG_VERBOSE << "Setting remote description: " << string(description);
 
 	if (description.type() == Description::Type::Rollback) {
@@ -182,7 +185,9 @@ void PeerConnection::setRemoteDescription(Description description) {
 		if (description.type() == Description::Type::Offer) {
 			// The ICE agent will automatically initiate a rollback when a peer that had previously
 			// created an offer receives an offer from the remote peer
-			setLocalDescription(Description::Type::Rollback);
+			impl()->rollbackLocalDescription();
+			impl()->changeSignalingState(SignalingState::Stable);
+			signalingState = SignalingState::Stable;
 			newSignalingState = SignalingState::HaveRemoteOffer;
 			break;
 		}
@@ -221,6 +226,7 @@ void PeerConnection::setRemoteDescription(Description description) {
 	impl()->processRemoteDescription(std::move(description));
 
 	impl()->changeSignalingState(newSignalingState);
+	signalingLock.unlock();
 
 	if (type == Description::Type::Offer) {
 		// This is an offer, we need to answer
@@ -233,6 +239,7 @@ void PeerConnection::setRemoteDescription(Description description) {
 }
 
 void PeerConnection::addRemoteCandidate(Candidate candidate) {
+	std::unique_lock signalingLock(impl()->signalingMutex);
 	PLOG_VERBOSE << "Adding remote candidate: " << string(candidate);
 	impl()->processRemoteCandidate(std::move(candidate));
 }