Bläddra i källkod

Improve RTP packet creation. Use mutex for RTP packet receiving

Michał Śledź 1 vecka sedan
förälder
incheckning
27859fd470
1 ändrade filer med 26 tillägg och 21 borttagningar
  1. 26 21
      test/track.cpp

+ 26 - 21
test/track.cpp

@@ -10,14 +10,17 @@
 #include "rtc/rtp.hpp"
 
 #include <atomic>
-#include <chrono>
+#include <cstring>
 #include <iostream>
 #include <memory>
+#include <mutex>
 #include <thread>
 
 using namespace rtc;
 using namespace std;
 
+static const auto RtpHeaderSize = 12;
+
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 void test_track() {
@@ -73,7 +76,8 @@ void test_track() {
 	shared_ptr<Track> t2;
 	string newTrackMid;
 	rtc::binary receivedRtpRaw;
-	pc2.onTrack([&t2, &newTrackMid, &receivedRtpRaw](shared_ptr<Track> t) {
+	std::mutex receivedRtpRawMtx;
+	pc2.onTrack([&t2, &newTrackMid, &receivedRtpRaw, &receivedRtpRawMtx](shared_ptr<Track> t) {
 		string mid = t->mid();
 		cout << "Track 2: Received track with mid \"" << mid << "\"" << endl;
 		if (mid != newTrackMid) {
@@ -87,8 +91,9 @@ void test_track() {
 		    [mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is closed" << endl; });
 
 		t->onMessage(
-		    [&receivedRtpRaw](rtc::binary message) {
+		    [&receivedRtpRaw, &receivedRtpRawMtx](rtc::binary message) {
 			    // This is an RTP packet
+			    std::lock_guard<std::mutex> lock(receivedRtpRawMtx);
 			    receivedRtpRaw = message;
 		    },
 		    nullptr);
@@ -149,15 +154,17 @@ void test_track() {
 	if (!at2 || !at2->isOpen() || !t1->isOpen())
 		throw runtime_error("Renegotiated track is not open");
 
-	RtpHeader rtp;
-	rtp._first = 0;
-	rtp.preparePacket();
-	rtp.setPayloadType(96);
-	rtp.setSeqNumber(1);
-	rtp.setTimestamp(3000);
-	rtp.setSsrc(2468);
-
-	if (!t1->send(reinterpret_cast<const std::byte *>(&rtp), sizeof(rtp))) {
+	std::vector<std::byte> payload = {std::byte{0}, std::byte{1}, std::byte{2}, std::byte{3}};
+	std::vector<std::byte> rtpRaw(RtpHeaderSize + payload.size());
+	auto *rtp = (RtpHeader *)rtpRaw.data();
+	rtp->setPayloadType(96);
+	rtp->setSeqNumber(1);
+	rtp->setTimestamp(3000);
+	rtp->setSsrc(2468);
+	rtp->preparePacket();
+	std::memcpy(rtpRaw.data() + RtpHeaderSize, payload.data(), payload.size());
+
+	if (!t1->send(rtpRaw.data(), rtpRaw.size())) {
 		throw runtime_error("Couldn't send RTP packet");
 	}
 
@@ -166,19 +173,17 @@ void test_track() {
 	while (count != 4 && receivedRtpRaw.empty() && attempts--)
 		this_thread::sleep_for(1s);
 
+	// make sure RTP packet was copied into receivedRtpRaw
+	std::lock_guard<std::mutex> lock(receivedRtpRawMtx);
+
 	if (receivedRtpRaw.empty()) {
 		throw runtime_error("Didn't receive RTP packet on pc2");
 	}
 
-	RtpHeader *receivedRtp = reinterpret_cast<RtpHeader *>(receivedRtpRaw.data());
-
-	if (receivedRtp->version() != rtp.version() || receivedRtp->padding() != rtp.padding() ||
-	    receivedRtp->extension() != rtp.extension() ||
-	    receivedRtp->csrcCount() != rtp.csrcCount() || receivedRtp->marker() != rtp.marker() ||
-	    receivedRtp->payloadType() != rtp.payloadType() ||
-	    receivedRtp->seqNumber() != rtp.seqNumber() ||
-	    receivedRtp->timestamp() != rtp.timestamp() || receivedRtp->ssrc() != rtp.ssrc()) {
-		throw runtime_error("Received RTP packet is different than the packet that was sent");
+	if (receivedRtpRaw.size() != rtpRaw.size() ||
+	    memcmp(receivedRtpRaw.data(), rtpRaw.data(), rtpRaw.size()) != 0) {
+		throw runtime_error(
+		    "Received RTP packet is different than the packet that was sent memcmp");
 	}
 
 	// Delay close of peer 2 to check closing works properly