Browse Source

Moved rtp.hpp into rtcp.hpp and rtcp.cpp

Paul-Louis Ageneau 4 years ago
parent
commit
75b0b6dcdf
5 changed files with 141 additions and 111 deletions
  1. 1 0
      CMakeLists.txt
  2. 59 0
      include/rtc/rtcp.hpp
  3. 0 1
      include/rtc/rtp.hp
  4. 1 1
      include/rtc/track.hpp
  5. 80 109
      src/rtcp.cpp

+ 1 - 0
CMakeLists.txt

@@ -51,6 +51,7 @@ set(LIBDATACHANNEL_SOURCES
 	${CMAKE_CURRENT_SOURCE_DIR}/src/message.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/peerconnection.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/rtc.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/rtcp.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/sctptransport.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/threadpool.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/tls.cpp

+ 59 - 0
include/rtc/rtcp.hpp

@@ -0,0 +1,59 @@
+/**
+ * Copyright (c) 2020 Staz M
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_RTCP_H
+#define RTC_RTCP_H
+
+#include "include.hpp"
+#include "log.hpp"
+#include "message.hpp"
+
+namespace rtc {
+
+typedef uint32_t SSRC;
+
+class RtcpHandler {
+public:
+	virtual void onOutgoing(std::function<void(rtc::message_ptr)> cb) = 0;
+	virtual std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) = 0;
+};
+
+// An RtcpSession can be plugged into a Track to handle the whole RTCP session
+class RtcpSession : public RtcpHandler {
+public:
+	void onOutgoing(std::function<void(rtc::message_ptr)> cb) override;
+
+	std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) override;
+	void requestBitrate(unsigned int newBitrate);
+
+private:
+	void pushREMB(unsigned int bitrate);
+	void pushRR(unsigned int lastSR_delay);
+	void tx(message_ptr msg);
+
+	unsigned int mRequestedBitrate = 0;
+	synchronized_callback<rtc::message_ptr> mTxCallback;
+	SSRC mSsrc = 0;
+	uint32_t mGreatestSeqNo = 0;
+	uint64_t mSyncRTPTS, mSyncNTPTS;
+};
+
+} // namespace rtc
+
+#endif // RTC_RTCP_H

+ 0 - 1
include/rtc/rtp.hp

@@ -1 +0,0 @@
-

+ 1 - 1
include/rtc/track.hpp

@@ -24,7 +24,7 @@
 #include "include.hpp"
 #include "message.hpp"
 #include "queue.hpp"
-#include "rtp.hpp"
+#include "rtcp.hpp"
 
 #include <atomic>
 #include <variant>

+ 80 - 109
include/rtc/rtp.hpp → src/rtcp.cpp

@@ -17,15 +17,9 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#ifndef RTC_RTPL_H
-#define RTC_RTPL_H
-
-#include "include.hpp"
-#include "log.hpp"
-#include "message.hpp"
+#include "rtcp.hpp"
 
 #include <cmath>
-#include <functional>
 #include <iostream>
 #include <utility>
 
@@ -45,10 +39,6 @@
 
 namespace rtc {
 
-using std::size_t;
-
-typedef uint32_t SSRC;
-
 #pragma pack(push, 1)
 
 struct RTCP_ReportBlock {
@@ -338,122 +328,103 @@ struct RTCP_REMB {
 
 #pragma pack(pop)
 
-class RtcpHandler {
-public:
-	virtual void onOutgoing(std::function<void(rtc::message_ptr)> cb) = 0;
-	virtual std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) = 0;
-};
-
-class RtcpSession : public RtcpHandler {
-private:
-	std::function<void(RTP)> onPacketCB;
-	unsigned int requestedBitrate = 0;
-	synchronized_callback<rtc::message_ptr> txCB;
-	SSRC ssrc = 0;
-	uint32_t greatestSeqNo = 0;
-	uint64_t syncRTPTS, syncNTPTS;
-
-public:
-	void onOutgoing(std::function<void(rtc::message_ptr)> cb) override { txCB = cb; }
-
-	std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) override {
-		if (ptr->type == rtc::Message::Type::Binary) {
-			RTP *rtp = (RTP *)ptr->data();
-
-			// https://tools.ietf.org/html/rfc3550#appendix-A.1
-			if (rtp->version() != 2) {
-				PLOG_WARNING << "RTP packet is not version 2";
-
-				return std::nullopt;
-			}
-			if (rtp->payloadType() == 201 || rtp->payloadType() == 200) {
-				PLOG_WARNING << "RTP packet has a payload type indicating RR/SR";
-
-				return std::nullopt;
-			}
+void RtcpSession::onOutgoing(std::function<void(rtc::message_ptr)> cb) { mTxCallback = cb; }
 
-			// TODO Implement the padding bit
-			if (rtp->padding()) {
-				PLOG_WARNING << "Padding processing not implemented";
-			}
+std::optional<rtc::message_ptr> RtcpSession::incoming(rtc::message_ptr ptr) {
+	if (ptr->type == rtc::Message::Type::Binary) {
+		RTP *rtp = (RTP *)ptr->data();
 
-			ssrc = ntohl(rtp->ssrc);
+		// https://tools.ietf.org/html/rfc3550#appendix-A.1
+		if (rtp->version() != 2) {
+			PLOG_WARNING << "RTP packet is not version 2";
 
-			uint32_t seqNo = rtp->seqNumber();
-			// uint32_t rtpTS = rtp->getTS();
-
-			if (greatestSeqNo < seqNo)
-				greatestSeqNo = seqNo;
-
-			return ptr;
+			return std::nullopt;
 		}
+		if (rtp->payloadType() == 201 || rtp->payloadType() == 200) {
+			PLOG_WARNING << "RTP packet has a payload type indicating RR/SR";
 
-		assert(ptr->type == rtc::Message::Type::Control);
-		auto rr = (RTCP_RR *)ptr->data();
-		if (rr->header.payloadType() == 201) {
-			// RR
-			ssrc = rr->getSenderSSRC();
-			rr->print();
-			std::cout << std::endl;
-		} else if (rr->header.payloadType() == 200) {
-			// SR
-			ssrc = rr->getSenderSSRC();
-			auto sr = (RTCP_SR *)ptr->data();
-			syncRTPTS = sr->rtpTimestamp();
-			syncNTPTS = sr->ntpTimestamp();
-			sr->print();
-			std::cout << std::endl;
+			return std::nullopt;
+		}
 
-			// TODO For the time being, we will send RR's/REMB's when we get an SR
-			pushRR(0);
-			if (requestedBitrate > 0)
-				pushREMB(requestedBitrate);
+		// TODO Implement the padding bit
+		if (rtp->padding()) {
+			PLOG_WARNING << "Padding processing not implemented";
 		}
-		return std::nullopt;
-	}
 
-	void requestBitrate(unsigned int newBitrate) {
-		this->requestedBitrate = newBitrate;
+		mSsrc = ntohl(rtp->ssrc);
 
-		PLOG_DEBUG << "[GOOG-REMB] Requesting bitrate: " << newBitrate << std::endl;
-		pushREMB(newBitrate);
-	}
+		uint32_t seqNo = rtp->seqNumber();
+		// uint32_t rtpTS = rtp->getTS();
 
-private:
-	void pushREMB(unsigned int bitrate) {
-		rtc::message_ptr msg =
-		    rtc::make_message(RTCP_REMB::sizeWithSSRCs(1), rtc::Message::Type::Control);
-		auto remb = (RTCP_REMB *)msg->data();
-		remb->preparePacket(ssrc, 1, bitrate);
-		remb->setSSRC(0, ssrc);
-		remb->print();
-		std::cout << std::endl;
+		if (mGreatestSeqNo < seqNo)
+			mGreatestSeqNo = seqNo;
 
-		tx(msg);
+		return ptr;
 	}
 
-	void pushRR(unsigned int lastSR_delay) {
-		// std::cout << "size " << RTCP_RR::sizeWithReportBlocks(1) << std::endl;
-		auto msg = rtc::make_message(RTCP_RR::sizeWithReportBlocks(1), rtc::Message::Type::Control);
-		auto rr = (RTCP_RR *)msg->data();
-		rr->preparePacket(ssrc, 1);
-		rr->getReportBlock(0)->preparePacket(ssrc, 0, 0, greatestSeqNo, 0, 0, syncNTPTS,
-		                                     lastSR_delay);
+	assert(ptr->type == rtc::Message::Type::Control);
+	auto rr = (RTCP_RR *)ptr->data();
+	if (rr->header.payloadType() == 201) {
+		// RR
+		mSsrc = rr->getSenderSSRC();
 		rr->print();
 		std::cout << std::endl;
+	} else if (rr->header.payloadType() == 200) {
+		// SR
+		mSsrc = rr->getSenderSSRC();
+		auto sr = (RTCP_SR *)ptr->data();
+		mSyncRTPTS = sr->rtpTimestamp();
+		mSyncNTPTS = sr->ntpTimestamp();
+		sr->print();
+		std::cout << std::endl;
 
-		tx(msg);
+		// TODO For the time being, we will send RR's/REMB's when we get an SR
+		pushRR(0);
+		if (mRequestedBitrate > 0)
+			pushREMB(mRequestedBitrate);
 	}
-
-	void tx(message_ptr msg) {
-		try {
-			txCB(msg);
-		} catch (const std::exception &e) {
-			LOG_DEBUG << "RTCP tx failed: " << e.what();
-		}
+	return std::nullopt;
+}
+
+void RtcpSession::requestBitrate(unsigned int newBitrate) {
+	mRequestedBitrate = newBitrate;
+
+	PLOG_DEBUG << "[GOOG-REMB] Requesting bitrate: " << newBitrate << std::endl;
+	pushREMB(newBitrate);
+}
+
+void RtcpSession::pushREMB(unsigned int bitrate) {
+	rtc::message_ptr msg =
+	    rtc::make_message(RTCP_REMB::sizeWithSSRCs(1), rtc::Message::Type::Control);
+	auto remb = (RTCP_REMB *)msg->data();
+	remb->preparePacket(mSsrc, 1, bitrate);
+	remb->setSSRC(0, mSsrc);
+	remb->print();
+	std::cout << std::endl;
+
+	tx(msg);
+}
+
+void RtcpSession::pushRR(unsigned int lastSR_delay) {
+	// std::cout << "size " << RTCP_RR::sizeWithReportBlocks(1) << std::endl;
+	auto msg = rtc::make_message(RTCP_RR::sizeWithReportBlocks(1), rtc::Message::Type::Control);
+	auto rr = (RTCP_RR *)msg->data();
+	rr->preparePacket(mSsrc, 1);
+	rr->getReportBlock(0)->preparePacket(mSsrc, 0, 0, mGreatestSeqNo, 0, 0, mSyncNTPTS,
+	                                     lastSR_delay);
+	rr->print();
+	std::cout << std::endl;
+
+	tx(msg);
+}
+
+void RtcpSession::tx(message_ptr msg) {
+	try {
+		mTxCallback(msg);
+	} catch (const std::exception &e) {
+		LOG_DEBUG << "RTCP tx failed: " << e.what();
 	}
-};
+}
 
 } // namespace rtc
 
-#endif // RTC_RTPL_H