2
0
Эх сурвалжийг харах

Removed start time in packetizer and deprecated related methods

Paul-Louis Ageneau 2 жил өмнө
parent
commit
a89e29547d

+ 10 - 19
include/rtc/rtcpsrreporter.hpp

@@ -28,23 +28,10 @@
 namespace rtc {
 
 class RTC_CPP_EXPORT RtcpSrReporter final : public MediaHandlerElement {
-	bool needsToReport = false;
-
-	uint32_t packetCount = 0;
-	uint32_t payloadOctets = 0;
-	double timeOffset = 0;
-
-	uint32_t mPreviousReportedTimestamp = 0;
-
 	void addToReport(RtpHeader *rtp, uint32_t rtpSize);
 	message_ptr getSenderReport(uint32_t timestamp);
 
 public:
-	static uint64_t secondsToNTP(double seconds);
-
-	/// Timestamp of previous sender report
-	const uint32_t &previousReportedTimestamp = mPreviousReportedTimestamp;
-
 	/// RTP configuration
 	const shared_ptr<RtpPacketizationConfig> rtpConfig;
 
@@ -53,14 +40,18 @@ public:
 	ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
 	                                                    message_ptr control) override;
 
-	/// Set `needsToReport` flag. Sender report will be sent before next RTP packet with same
-	/// timestamp.
+	uint32_t lastReportedTimestamp() const;
 	void setNeedsToReport();
 
-	/// Set offset to compute NTS for RTCP SR packets. Offset represents relation between real start
-	/// time and timestamp of the stream in RTP packets
-	/// @note `time_offset = rtpConfig->startTime - rtpConfig->timestampToSeconds(rtpConfig->timestamp)`
-	void startRecording();
+	// deprecated, do not call
+	[[deprecated]] void startRecording();
+	[[deprecated]] uint32_t previousReportedTimestamp() const { return lastReportedTimestamp(); }
+
+private:
+	uint32_t mPacketCount = 0;
+	uint32_t mPayloadOctets = 0;
+	uint32_t mLastReportedTimestamp = 0;
+	bool mNeedsToReport = false;
 };
 
 } // namespace rtc

+ 18 - 29
include/rtc/rtppacketizationconfig.hpp

@@ -25,27 +25,24 @@
 
 namespace rtc {
 
-/// RTP configuration used in packetization process
+// RTP configuration used in packetization process
 class RTC_CPP_EXPORT RtpPacketizationConfig {
-	uint32_t mStartTimestamp = 0;
-	double mStartTime = 0;
-	RtpPacketizationConfig(const RtpPacketizationConfig &) = delete;
-
 public:
 	const SSRC ssrc;
 	const std::string cname;
 	const uint8_t payloadType;
 	const uint32_t clockRate;
-	const double &startTime = mStartTime;
-	const uint32_t &startTimestamp = mStartTimestamp;
 	const uint8_t videoOrientationId;
 
-	/// current sequence number
+	// current sequence number
 	uint16_t sequenceNumber;
 
-	/// current timestamp
+	// current timestamp
 	uint32_t timestamp;
 
+	// start timestamp
+	uint32_t startTimestamp;
+
 	/// Current video orientation
 	///
 	/// Bit#       7  6  5  4  3  2  1  0
@@ -66,35 +63,18 @@ public:
 	///   3 - 270 degrees
 	uint8_t videoOrientation = 0;
 
-	// For backward compatibility, do not use
-	const double &startTime_s = mStartTime;
-
-	enum class EpochStart : uint64_t {
-		T1970 = 2208988800, // number of seconds between 1970 and 1900
-		T1900 = 0
-	};
-
-	/// Creates relation between time and timestamp mapping given start time and start timestamp
-	/// @param startTime Start time of the stream
-	/// @param epochStart Type of used epoch
-	/// @param startTimestamp Corresponding timestamp for given start time (current timestamp will
-	/// be used if value is nullopt)
-	void setStartTime(double startTime, EpochStart epochStart,
-	                  optional<uint32_t> startTimestamp = std::nullopt);
-
 	/// Construct RTP configuration used in packetization process
 	/// @param ssrc SSRC of source
 	/// @param cname CNAME of source
 	/// @param payloadType Payload type of source
 	/// @param clockRate Clock rate of source used in timestamps
-	/// @param sequenceNumber Initial sequence number of RTP packets (random number is choosed if
 	/// nullopt)
-	/// @param timestamp Initial timastamp of RTP packets (random number is choosed if nullopt)
+	/// @param videoOrientationId Video orientation (see above)
 	RtpPacketizationConfig(SSRC ssrc, std::string cname, uint8_t payloadType, uint32_t clockRate,
-	                       optional<uint16_t> sequenceNumber = std::nullopt,
-	                       optional<uint32_t> timestamp = std::nullopt,
 	                       uint8_t videoOrientationId = 0);
 
+	RtpPacketizationConfig(const RtpPacketizationConfig &) = delete;
+
 	/// Convert timestamp to seconds
 	/// @param timestamp Timestamp
 	/// @param clockRate Clock rate for timestamp calculation
@@ -112,6 +92,15 @@ public:
 	/// Convert seconds to timestamp
 	/// @param seconds Number of seconds
 	uint32_t secondsToTimestamp(double seconds);
+
+	// deprecated, do not use
+	double startTime = 0.;
+	enum class EpochStart : uint64_t {
+		T1970 = 2208988800, // number of seconds between 1970 and 1900
+		T1900 = 0
+	};
+	[[deprecated]] void setStartTime(double startTime, EpochStart epochStart,
+	                                 optional<uint32_t> startTimestamp = std::nullopt);
 };
 
 } // namespace rtc

+ 33 - 22
src/rtcpsrreporter.cpp

@@ -21,13 +21,25 @@
 #include "rtcpsrreporter.hpp"
 
 #include <cassert>
+#include <chrono>
 #include <cmath>
 
+namespace {
+
+uint64_t ntp_time() {
+	const auto now = std::chrono::system_clock::now();
+	const double secs = std::chrono::duration<double>(now.time_since_epoch()).count();
+	// Assume the epoch is 01/01/1970 and adds the number of seconds between 1900 and 1970
+	return uint64_t(std::floor((secs + 2208988800.) * double(uint64_t(1) << 32)));
+}
+
+} // namespace
+
 namespace rtc {
 
 ChainedOutgoingProduct RtcpSrReporter::processOutgoingBinaryMessage(ChainedMessagesProduct messages,
                                                                     message_ptr control) {
-	if (needsToReport) {
+	if (std::exchange(mNeedsToReport, false)) {
 		auto timestamp = rtpConfig->timestamp;
 		auto sr = getSenderReport(timestamp);
 		if (control) {
@@ -35,7 +47,6 @@ ChainedOutgoingProduct RtcpSrReporter::processOutgoingBinaryMessage(ChainedMessa
 		} else {
 			control = sr;
 		}
-		needsToReport = false;
 	}
 	for (auto message : *messages) {
 		auto rtp = reinterpret_cast<RtpHeader *>(message->data());
@@ -44,37 +55,26 @@ ChainedOutgoingProduct RtcpSrReporter::processOutgoingBinaryMessage(ChainedMessa
 	return {messages, control};
 }
 
-void RtcpSrReporter::startRecording() {
-	mPreviousReportedTimestamp = rtpConfig->timestamp;
-	timeOffset = rtpConfig->startTime - rtpConfig->timestampToSeconds(rtpConfig->timestamp);
-}
-
 void RtcpSrReporter::addToReport(RtpHeader *rtp, uint32_t rtpSize) {
-	packetCount += 1;
+	mPacketCount += 1;
 	assert(!rtp->padding());
-	payloadOctets += rtpSize - uint32_t(rtp->getSize());
+	mPayloadOctets += rtpSize - uint32_t(rtp->getSize());
 }
 
 RtcpSrReporter::RtcpSrReporter(shared_ptr<RtpPacketizationConfig> rtpConfig)
-    : MediaHandlerElement(), rtpConfig(rtpConfig) {}
-
-uint64_t RtcpSrReporter::secondsToNTP(double seconds) {
-	return uint64_t(std::round(seconds * double(uint64_t(1) << 32)));
+    : MediaHandlerElement(), rtpConfig(rtpConfig) {
+	mLastReportedTimestamp = rtpConfig->timestamp;
 }
 
-void RtcpSrReporter::setNeedsToReport() { needsToReport = true; }
-
 message_ptr RtcpSrReporter::getSenderReport(uint32_t timestamp) {
 	auto srSize = RtcpSr::Size(0);
 	auto msg = make_message(srSize + RtcpSdes::Size({{uint8_t(rtpConfig->cname.size())}}),
 	                        Message::Control);
 	auto sr = reinterpret_cast<RtcpSr *>(msg->data());
-	auto timestamp_s = rtpConfig->timestampToSeconds(timestamp);
-	auto currentTime = timeOffset + timestamp_s;
-	sr->setNtpTimestamp(secondsToNTP(currentTime));
+	sr->setNtpTimestamp(ntp_time());
 	sr->setRtpTimestamp(timestamp);
-	sr->setPacketCount(packetCount);
-	sr->setOctetCount(payloadOctets);
+	sr->setPacketCount(mPacketCount);
+	sr->setOctetCount(mPayloadOctets);
 	sr->preparePacket(rtpConfig->ssrc, 0);
 
 	auto sdes = reinterpret_cast<RtcpSdes *>(msg->data() + srSize);
@@ -85,11 +85,22 @@ message_ptr RtcpSrReporter::getSenderReport(uint32_t timestamp) {
 	item->setText(rtpConfig->cname);
 	sdes->preparePacket(1);
 
-	mPreviousReportedTimestamp = timestamp;
-
+	mLastReportedTimestamp = timestamp;
 	return msg;
 }
 
+void RtcpSrReporter::setNeedsToReport() {
+	mNeedsToReport = true;
+}
+
+void RtcpSrReporter::startRecording() {
+	// Dummy
+}
+
+uint32_t RtcpSrReporter::lastReportedTimestamp() const {
+	return mLastReportedTimestamp;
+}
+
 } // namespace rtc
 
 #endif /* RTC_ENABLE_MEDIA */

+ 15 - 28
src/rtppacketizationconfig.cpp

@@ -25,35 +25,13 @@
 namespace rtc {
 
 RtpPacketizationConfig::RtpPacketizationConfig(SSRC ssrc, string cname, uint8_t payloadType,
-                                               uint32_t clockRate,
-                                               optional<uint16_t> sequenceNumber,
-                                               optional<uint32_t> timestamp,
-                                               uint8_t videoOrientationId)
-    : ssrc(ssrc), cname(cname), payloadType(payloadType), clockRate(clockRate), videoOrientationId(videoOrientationId) {
+                                               uint32_t clockRate, uint8_t videoOrientationId)
+    : ssrc(ssrc), cname(cname), payloadType(payloadType), clockRate(clockRate),
+      videoOrientationId(videoOrientationId) {
 	assert(clockRate > 0);
-	srand((unsigned)time(NULL));
-	if (sequenceNumber.has_value()) {
-		this->sequenceNumber = sequenceNumber.value();
-	} else {
-		this->sequenceNumber = rand();
-	}
-	if (timestamp.has_value()) {
-		this->timestamp = timestamp.value();
-	} else {
-		this->timestamp = rand();
-	}
-	this->mStartTimestamp = this->timestamp;
-}
-
-void RtpPacketizationConfig::setStartTime(double startTime, EpochStart epochStart,
-                                          optional<uint32_t> startTimestamp) {
-	this->mStartTime = startTime + double(static_cast<uint64_t>(epochStart));
-	if (startTimestamp.has_value()) {
-		this->mStartTimestamp = startTimestamp.value();
-		timestamp = this->startTimestamp;
-	} else {
-		this->mStartTimestamp = timestamp;
-	}
+	// TODO: random sequence ans timetamp
+	sequenceNumber = 0;
+	timestamp = startTimestamp = 0;
 }
 
 double RtpPacketizationConfig::getSecondsFromTimestamp(uint32_t timestamp, uint32_t clockRate) {
@@ -72,6 +50,15 @@ uint32_t RtpPacketizationConfig::secondsToTimestamp(double seconds) {
 	return RtpPacketizationConfig::getTimestampFromSeconds(seconds, clockRate);
 }
 
+void RtpPacketizationConfig::setStartTime(double startTime,
+										  EpochStart epochStart,
+                                          optional<uint32_t> startTimestamp) {
+	// Deprecated dummy function
+	this->startTime = startTime + double(static_cast<uint64_t>(epochStart));
+	if (startTimestamp.has_value())
+		this->timestamp = this->startTimestamp = startTimestamp.value();
+}
+
 } // namespace rtc
 
 #endif /* RTC_ENABLE_MEDIA */