Browse Source

Used synchronized callbacks for PeerConnection

Paul-Louis Ageneau 5 years ago
parent
commit
5d57b4e214
4 changed files with 40 additions and 39 deletions
  1. 0 19
      include/rtc/channel.hpp
  2. 21 0
      include/rtc/include.hpp
  3. 5 5
      include/rtc/peerconnection.hpp
  4. 14 15
      src/peerconnection.cpp

+ 0 - 19
include/rtc/channel.hpp

@@ -54,25 +54,6 @@ protected:
 	virtual void triggerSent();
 
 private:
-	template <typename... P> class synchronized_callback {
-	public:
-		synchronized_callback &operator=(std::function<void(P...)> func) {
-			std::lock_guard<std::recursive_mutex> lock(mutex);
-			callback = func;
-			return *this;
-		}
-
-		void operator()(P... args) {
-			std::lock_guard<std::recursive_mutex> lock(mutex);
-			if (callback)
-				callback(args...);
-		}
-
-	private:
-		std::function<void(P...)> callback;
-		std::recursive_mutex mutex;
-	};
-
 	synchronized_callback<> mOpenCallback;
 	synchronized_callback<> mClosedCallback;
 	synchronized_callback<const string &> mErrorCallback;

+ 21 - 0
include/rtc/include.hpp

@@ -20,7 +20,9 @@
 #define RTC_INCLUDE_H
 
 #include <cstddef>
+#include <functional>
 #include <memory>
+#include <mutex>
 #include <optional>
 #include <string>
 #include <vector>
@@ -49,6 +51,25 @@ const uint16_t DEFAULT_SCTP_PORT = 5000; // SCTP port to use by default
 
 template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
 template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;
+
+template <typename... P> class synchronized_callback {
+public:
+	synchronized_callback &operator=(std::function<void(P...)> func) {
+		std::lock_guard<std::recursive_mutex> lock(mutex);
+		callback = func;
+		return *this;
+	}
+
+	void operator()(P... args) {
+		std::lock_guard<std::recursive_mutex> lock(mutex);
+		if (callback)
+			callback(args...);
+	}
+
+private:
+	std::function<void(P...)> callback;
+	std::recursive_mutex mutex;
+};
 }
 
 #endif

+ 5 - 5
include/rtc/peerconnection.hpp

@@ -115,11 +115,11 @@ private:
 	std::atomic<State> mState;
 	std::atomic<GatheringState> mGatheringState;
 
-	std::function<void(std::shared_ptr<DataChannel> dataChannel)> mDataChannelCallback;
-	std::function<void(const Description &description)> mLocalDescriptionCallback;
-	std::function<void(const Candidate &candidate)> mLocalCandidateCallback;
-	std::function<void(State state)> mStateChangeCallback;
-	std::function<void(GatheringState state)> mGatheringStateChangeCallback;
+	synchronized_callback<std::shared_ptr<DataChannel>> mDataChannelCallback;
+	synchronized_callback<const Description &> mLocalDescriptionCallback;
+	synchronized_callback<const Candidate &> mLocalCandidateCallback;
+	synchronized_callback<State> mStateChangeCallback;
+	synchronized_callback<GatheringState> mGatheringStateChangeCallback;
 };
 
 } // namespace rtc

+ 14 - 15
src/peerconnection.cpp

@@ -28,7 +28,6 @@ namespace rtc {
 
 using namespace std::placeholders;
 
-using std::function;
 using std::shared_ptr;
 using std::weak_ptr;
 
@@ -358,41 +357,41 @@ void PeerConnection::processLocalDescription(Description description) {
 	mLocalDescription->setFingerprint(mCertificate->fingerprint());
 	mLocalDescription->setSctpPort(remoteSctpPort.value_or(DEFAULT_SCTP_PORT));
 
-	if (mLocalDescriptionCallback)
-		mLocalDescriptionCallback(*mLocalDescription);
+	mLocalDescriptionCallback(*mLocalDescription);
 }
 
 void PeerConnection::processLocalCandidate(weak_ptr<PeerConnection> weak_this, Candidate candidate) {
-  auto strong_this = weak_this.lock();
-  if (!strong_this) return;
+	auto strong_this = weak_this.lock();
+	if (!strong_this)
+		return;
 
 	if (!mLocalDescription)
 		throw std::logic_error("Got a local candidate without local description");
 
 	mLocalDescription->addCandidate(candidate);
 
-	if (mLocalCandidateCallback)
-		mLocalCandidateCallback(candidate);
+	mLocalCandidateCallback(candidate);
 }
 
 void PeerConnection::triggerDataChannel(weak_ptr<PeerConnection> weak_this, weak_ptr<DataChannel> weakDataChannel) {
-  auto strong_this = weak_this.lock();
-  if (!strong_this) return;
+	auto strong_this = weak_this.lock();
+	if (!strong_this)
+		return;
 
-  auto dataChannel = weakDataChannel.lock();
-  if (!dataChannel) return;
+	auto dataChannel = weakDataChannel.lock();
+	if (!dataChannel)
+		return;
 
-	if (mDataChannelCallback)
-		mDataChannelCallback(dataChannel);
+	mDataChannelCallback(dataChannel);
 }
 
 void PeerConnection::changeState(State state) {
-	if (mState.exchange(state) != state && mStateChangeCallback)
+	if (mState.exchange(state) != state)
 		mStateChangeCallback(state);
 }
 
 void PeerConnection::changeGatheringState(GatheringState state) {
-	if (mGatheringState.exchange(state) != state && mGatheringStateChangeCallback)
+	if (mGatheringState.exchange(state) != state)
 		mGatheringStateChangeCallback(state);
 }