|
@@ -10,14 +10,17 @@
|
|
#include "rtc/rtp.hpp"
|
|
#include "rtc/rtp.hpp"
|
|
|
|
|
|
#include <atomic>
|
|
#include <atomic>
|
|
-#include <chrono>
|
|
|
|
|
|
+#include <cstring>
|
|
#include <iostream>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <memory>
|
|
|
|
+#include <mutex>
|
|
#include <thread>
|
|
#include <thread>
|
|
|
|
|
|
using namespace rtc;
|
|
using namespace rtc;
|
|
using namespace std;
|
|
using namespace std;
|
|
|
|
|
|
|
|
+static const auto RtpHeaderSize = 12;
|
|
|
|
+
|
|
template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
|
|
template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
|
|
|
|
|
|
void test_track() {
|
|
void test_track() {
|
|
@@ -73,7 +76,8 @@ void test_track() {
|
|
shared_ptr<Track> t2;
|
|
shared_ptr<Track> t2;
|
|
string newTrackMid;
|
|
string newTrackMid;
|
|
rtc::binary receivedRtpRaw;
|
|
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();
|
|
string mid = t->mid();
|
|
cout << "Track 2: Received track with mid \"" << mid << "\"" << endl;
|
|
cout << "Track 2: Received track with mid \"" << mid << "\"" << endl;
|
|
if (mid != newTrackMid) {
|
|
if (mid != newTrackMid) {
|
|
@@ -87,8 +91,9 @@ void test_track() {
|
|
[mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is closed" << endl; });
|
|
[mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is closed" << endl; });
|
|
|
|
|
|
t->onMessage(
|
|
t->onMessage(
|
|
- [&receivedRtpRaw](rtc::binary message) {
|
|
|
|
|
|
+ [&receivedRtpRaw, &receivedRtpRawMtx](rtc::binary message) {
|
|
// This is an RTP packet
|
|
// This is an RTP packet
|
|
|
|
+ std::lock_guard<std::mutex> lock(receivedRtpRawMtx);
|
|
receivedRtpRaw = message;
|
|
receivedRtpRaw = message;
|
|
},
|
|
},
|
|
nullptr);
|
|
nullptr);
|
|
@@ -149,15 +154,17 @@ void test_track() {
|
|
if (!at2 || !at2->isOpen() || !t1->isOpen())
|
|
if (!at2 || !at2->isOpen() || !t1->isOpen())
|
|
throw runtime_error("Renegotiated track is not open");
|
|
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");
|
|
throw runtime_error("Couldn't send RTP packet");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -166,19 +173,17 @@ void test_track() {
|
|
while (count != 4 && receivedRtpRaw.empty() && attempts--)
|
|
while (count != 4 && receivedRtpRaw.empty() && attempts--)
|
|
this_thread::sleep_for(1s);
|
|
this_thread::sleep_for(1s);
|
|
|
|
|
|
|
|
+ // make sure RTP packet was copied into receivedRtpRaw
|
|
|
|
+ std::lock_guard<std::mutex> lock(receivedRtpRawMtx);
|
|
|
|
+
|
|
if (receivedRtpRaw.empty()) {
|
|
if (receivedRtpRaw.empty()) {
|
|
throw runtime_error("Didn't receive RTP packet on pc2");
|
|
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
|
|
// Delay close of peer 2 to check closing works properly
|