Murat Dogan 5 years ago
parent
commit
dee0074270

+ 3 - 3
include/rtc/peerconnection.hpp

@@ -90,9 +90,9 @@ public:
 	bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote);
 
 	// Stats
-	const unsigned int bytesSent();
-	const unsigned int bytesReceived();
-	const unsigned int rttInMs();
+	const size_t bytesSent();
+	const size_t bytesReceived();
+	const std::chrono::milliseconds rtt();
 
 private:
 	init_token mInitToken = Init::Token();

+ 5 - 5
src/peerconnection.cpp

@@ -579,25 +579,25 @@ bool PeerConnection::getSelectedCandidatePair(CandidateInfo *local, CandidateInf
 #endif
 }
 
-const unsigned int PeerConnection::bytesSent() {
+const size_t PeerConnection::bytesSent() {
 	auto sctpTransport = std::atomic_load(&mSctpTransport);
 	if (sctpTransport)
 		return sctpTransport->bytesSent();
 	return 0;
 }
 
-const unsigned int PeerConnection::bytesReceived() {
+const size_t PeerConnection::bytesReceived() {
 	auto sctpTransport = std::atomic_load(&mSctpTransport);
 	if (sctpTransport)
 		return sctpTransport->bytesReceived();
 	return 0;
 }
 
-const unsigned int PeerConnection::rttInMs() {
+const std::chrono::milliseconds PeerConnection::rtt() {
 	auto sctpTransport = std::atomic_load(&mSctpTransport);
 	if (sctpTransport)
-		return sctpTransport->rttInMs();
-	return 0;
+		return sctpTransport->rtt();
+	return std::chrono::milliseconds(0);
 }
 
 } // namespace rtc

+ 10 - 8
src/sctptransport.cpp

@@ -471,10 +471,11 @@ void SctpTransport::processData(const byte *data, size_t len, uint16_t sid, Payl
 
 	case PPID_STRING:
 		if (mPartialStringData.empty()) {
-			recv(make_message(data, data + len, Message::String, sid));
 			mBytesReceived += len;
+			recv(make_message(data, data + len, Message::String, sid));
 		} else {
 			mPartialStringData.insert(mPartialStringData.end(), data, data + len);
+			mBytesReceived += mPartialStringData.size();
 			recv(make_message(mPartialStringData.begin(), mPartialStringData.end(), Message::String,
 			                  sid));
 			mPartialStringData.clear();
@@ -494,10 +495,11 @@ void SctpTransport::processData(const byte *data, size_t len, uint16_t sid, Payl
 
 	case PPID_BINARY:
 		if (mPartialBinaryData.empty()) {
-			recv(make_message(data, data + len, Message::Binary, sid));
 			mBytesReceived += len;
+			recv(make_message(data, data + len, Message::Binary, sid));
 		} else {
 			mPartialBinaryData.insert(mPartialBinaryData.end(), data, data + len);
+			mBytesReceived += mPartialStringData.size();
 			recv(make_message(mPartialBinaryData.begin(), mPartialBinaryData.end(), Message::Binary,
 			                  sid));
 			mPartialBinaryData.clear();
@@ -586,16 +588,16 @@ void SctpTransport::clearStats() {
 	mBytesSent = 0;
 }
 
-const unsigned int SctpTransport::bytesSent() { return mBytesSent; }
+const size_t SctpTransport::bytesSent() { return mBytesSent; }
 
-const unsigned int SctpTransport::bytesReceived() { return mBytesReceived; }
+const size_t SctpTransport::bytesReceived() { return mBytesReceived; }
 
-const unsigned int SctpTransport::rttInMs() {
-	sctp_paddrinfo info= {0};
+const std::chrono::milliseconds SctpTransport::rtt() {
+	sctp_paddrinfo info = {0};
 	socklen_t optlen = sizeof(info);
 	if (usrsctp_getsockopt(mSock, IPPROTO_SCTP, SCTP_GET_PEER_ADDR_INFO, &info, &optlen))
-		return 0;
-	return info.spinfo_srtt;
+		return std::chrono::milliseconds(0);
+	return std::chrono::milliseconds(info.spinfo_srtt);
 }
 
 int SctpTransport::RecvCallback(struct socket *sock, union sctp_sockstore addr, void *data,

+ 4 - 4
src/sctptransport.hpp

@@ -56,9 +56,9 @@ public:
 
 	// Stats
 	void clearStats();
-	const unsigned int bytesSent();
-	const unsigned int bytesReceived();
-	const unsigned int rttInMs();
+	const size_t bytesSent();
+	const size_t bytesReceived();
+	const std::chrono::milliseconds rtt();
 
 private:
 	// Order seems wrong but these are the actual values
@@ -108,7 +108,7 @@ private:
 	std::atomic<State> mState;
 
 	// Stats
-	unsigned int mBytesSent = 0, mBytesReceived = 0;
+	std::atomic<size_t> mBytesSent = 0, mBytesReceived = 0;
 
 	binary mPartialRecv, mPartialStringData, mPartialBinaryData;
 

+ 6 - 4
test/p2p/answerer.cpp

@@ -129,10 +129,12 @@ int main(int argc, char **argv) {
 			}
 			CandidateInfo local, remote;
 			if (pc->getSelectedCandidatePair(&local, &remote)) {
-				cout << "Local: " << local.address << ":" << local.port << " " << local.type << " "
-				     << local.transportType << endl;
-				cout << "Remote: " << remote.address << ":" << remote.port << " " << remote.type
-				     << " " << remote.transportType << endl;
+				cout << "Local Candidate: " << local.address << ":" << local.port << " "
+				     << local.type << " " << local.transportType << endl;
+				cout << "Remote Candidate: " << remote.address << ":" << remote.port << " "
+				     << remote.type << " " << remote.transportType << endl;
+				cout << "Bytes Sent:" << pc->bytesSent() << " Bytes Received:" << pc->bytesReceived()
+				     << " Round-Trip Time:" << pc->rtt().count() << " ms" << endl;
 			} else
 				cout << "Could not get Candidate Pair Info" << endl;
 			break;

+ 2 - 0
test/p2p/offerer.cpp

@@ -133,6 +133,8 @@ int main(int argc, char **argv) {
 				     << local.transportType << endl;
 				cout << "Remote: " << remote.address << ":" << remote.port << " " << remote.type
 				     << " " << remote.transportType << endl;
+				cout << "Bytes Sent:" << pc->bytesSent() << " Bytes Received:" << pc->bytesReceived()
+				     << " Round-Trip Time:" << pc->rtt().count() << " ms" << endl;
 			} else
 				cout << "Could not get Candidate Pair Info" << endl;
 			break;