Browse Source

Added a mutex for the RTCP Handler

Staz M 4 years ago
parent
commit
e295fa6072
2 changed files with 14 additions and 5 deletions
  1. 2 0
      include/rtc/track.hpp
  2. 12 5
      src/track.cpp

+ 2 - 0
include/rtc/track.hpp

@@ -77,6 +77,8 @@ private:
 	std::atomic<bool> mIsClosed = false;
 	std::atomic<bool> mIsClosed = false;
 
 
 	Queue<message_ptr> mRecvQueue;
 	Queue<message_ptr> mRecvQueue;
+
+	std::shared_mutex mRtcpHandlerMutex;
 	std::shared_ptr<RtcpHandler> mRtcpHandler;
 	std::shared_ptr<RtcpHandler> mRtcpHandler;
 
 
 	friend class PeerConnection;
 	friend class PeerConnection;

+ 12 - 5
src/track.cpp

@@ -62,7 +62,8 @@ bool Track::send(message_variant data) {
 
 
 	auto message = make_message(std::move(data));
 	auto message = make_message(std::move(data));
 
 
-	if (mRtcpHandler) {
+    std::shared_lock lock(mRtcpHandlerMutex);
+    if (mRtcpHandler) {
 		message = mRtcpHandler->outgoing(message);
 		message = mRtcpHandler->outgoing(message);
 		if (!message)
 		if (!message)
 			return false;
 			return false;
@@ -122,7 +123,8 @@ void Track::incoming(message_ptr message) {
 		return;
 		return;
 	}
 	}
 
 
-	if (mRtcpHandler) {
+    std::shared_lock lock(mRtcpHandlerMutex);
+    if (mRtcpHandler) {
 		message = mRtcpHandler->incoming(message);
 		message = mRtcpHandler->incoming(message);
 		if (!message)
 		if (!message)
 			return;
 			return;
@@ -159,17 +161,22 @@ bool Track::outgoing([[maybe_unused]] message_ptr message) {
 }
 }
 
 
 void Track::setRtcpHandler(std::shared_ptr<RtcpHandler> handler) {
 void Track::setRtcpHandler(std::shared_ptr<RtcpHandler> handler) {
-	mRtcpHandler = std::move(handler);
+    std::unique_lock lock(mRtcpHandlerMutex);
+    mRtcpHandler = std::move(handler);
 	if (mRtcpHandler)
 	if (mRtcpHandler)
 		mRtcpHandler->onOutgoing(std::bind(&Track::outgoing, this, std::placeholders::_1));
 		mRtcpHandler->onOutgoing(std::bind(&Track::outgoing, this, std::placeholders::_1));
 }
 }
 
 
 bool Track::requestKeyframe() {
 bool Track::requestKeyframe() {
-	if (mRtcpHandler)
+    std::shared_lock lock(mRtcpHandlerMutex);
+    if (mRtcpHandler)
 		return mRtcpHandler->requestKeyframe();
 		return mRtcpHandler->requestKeyframe();
 	return false;
 	return false;
 }
 }
 
 
-std::shared_ptr<RtcpHandler> Track::getRtcpHandler() { return mRtcpHandler; }
+std::shared_ptr<RtcpHandler> Track::getRtcpHandler() {
+    std::shared_lock lock(mRtcpHandlerMutex);
+    return mRtcpHandler;
+}
 
 
 } // namespace rtc
 } // namespace rtc