Removed shared_ptr to PeerConnection in tests

This commit is contained in:
Paul-Louis Ageneau
2021-02-28 11:59:44 +01:00
parent dde79d78d4
commit 0638555fef
6 changed files with 123 additions and 174 deletions

View File

@ -54,20 +54,20 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
string randomId(size_t length); string randomId(size_t length);
int main(int argc, char **argv) try { int main(int argc, char **argv) try {
auto params = std::make_unique<Cmdline>(argc, argv); Cmdline params(argc, argv);
rtc::InitLogger(LogLevel::Info); rtc::InitLogger(LogLevel::Info);
Configuration config; Configuration config;
string stunServer = ""; string stunServer = "";
if (params->noStun()) { if (params.noStun()) {
cout << "No STUN server is configured. Only local hosts and public IP addresses supported." cout << "No STUN server is configured. Only local hosts and public IP addresses supported."
<< endl; << endl;
} else { } else {
if (params->stunServer().substr(0, 5).compare("stun:") != 0) { if (params.stunServer().substr(0, 5).compare("stun:") != 0) {
stunServer = "stun:"; stunServer = "stun:";
} }
stunServer += params->stunServer() + ":" + to_string(params->stunPort()); stunServer += params.stunServer() + ":" + to_string(params.stunPort());
cout << "Stun server is " << stunServer << endl; cout << "Stun server is " << stunServer << endl;
config.iceServers.emplace_back(stunServer); config.iceServers.emplace_back(stunServer);
} }
@ -129,11 +129,11 @@ int main(int argc, char **argv) try {
}); });
string wsPrefix = ""; string wsPrefix = "";
if (params->webSocketServer().substr(0, 5).compare("ws://") != 0) { if (params.webSocketServer().substr(0, 5).compare("ws://") != 0) {
wsPrefix = "ws://"; wsPrefix = "ws://";
} }
const string url = wsPrefix + params->webSocketServer() + ":" + const string url = wsPrefix + params.webSocketServer() + ":" +
to_string(params->webSocketPort()) + "/" + localId; to_string(params.webSocketPort()) + "/" + localId;
cout << "Url is " << url << endl; cout << "Url is " << url << endl;
ws->open(url); ws->open(url);
@ -251,3 +251,4 @@ string randomId(size_t length) {
generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); }); generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); });
return id; return id;
} }

View File

@ -42,53 +42,41 @@ size_t benchmark(milliseconds duration) {
// config1.iceServers.emplace_back("stun:stun.l.google.com:19302"); // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
// config1.mtu = 1500; // config1.mtu = 1500;
auto pc1 = std::make_shared<PeerConnection>(config1); PeerConnection pc1(config1);
Configuration config2; Configuration config2;
// config2.iceServers.emplace_back("stun:stun.l.google.com:19302"); // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
// config2.mtu = 1500; // config2.mtu = 1500;
auto pc2 = std::make_shared<PeerConnection>(config2); PeerConnection pc2(config2);
pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { pc1.onLocalDescription([&pc2](Description sdp) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
cout << "Description 1: " << sdp << endl; cout << "Description 1: " << sdp << endl;
pc2->setRemoteDescription(std::move(sdp)); pc2.setRemoteDescription(std::move(sdp));
}); });
pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { pc1.onLocalCandidate([&pc2](Candidate candidate) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
cout << "Candidate 1: " << candidate << endl; cout << "Candidate 1: " << candidate << endl;
pc2->addRemoteCandidate(std::move(candidate)); pc2.addRemoteCandidate(std::move(candidate));
}); });
pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; }); pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
pc1->onGatheringStateChange([](PeerConnection::GatheringState state) { pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 1: " << state << endl; cout << "Gathering state 1: " << state << endl;
}); });
pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { pc2.onLocalDescription([&pc1](Description sdp) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
cout << "Description 2: " << sdp << endl; cout << "Description 2: " << sdp << endl;
pc1->setRemoteDescription(std::move(sdp)); pc1.setRemoteDescription(std::move(sdp));
}); });
pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { pc2.onLocalCandidate([&pc1](Candidate candidate) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
cout << "Candidate 2: " << candidate << endl; cout << "Candidate 2: " << candidate << endl;
pc1->addRemoteCandidate(std::move(candidate)); pc1.addRemoteCandidate(std::move(candidate));
}); });
pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; }); pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
pc2->onGatheringStateChange([](PeerConnection::GatheringState state) { pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 2: " << state << endl; cout << "Gathering state 2: " << state << endl;
}); });
@ -101,7 +89,7 @@ size_t benchmark(milliseconds duration) {
steady_clock::time_point startTime, openTime, receivedTime, endTime; steady_clock::time_point startTime, openTime, receivedTime, endTime;
shared_ptr<DataChannel> dc2; shared_ptr<DataChannel> dc2;
pc2->onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr<DataChannel> dc) { pc2.onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr<DataChannel> dc) {
dc->onMessage([&receivedTime, &receivedSize](variant<binary, string> message) { dc->onMessage([&receivedTime, &receivedSize](variant<binary, string> message) {
if (holds_alternative<binary>(message)) { if (holds_alternative<binary>(message)) {
const auto &bin = get<binary>(message); const auto &bin = get<binary>(message);
@ -117,7 +105,7 @@ size_t benchmark(milliseconds duration) {
}); });
startTime = steady_clock::now(); startTime = steady_clock::now();
auto dc1 = pc1->createDataChannel("benchmark"); auto dc1 = pc1.createDataChannel("benchmark");
dc1->onOpen([wdc1 = make_weak_ptr(dc1), &messageData, &openTime]() { dc1->onOpen([wdc1 = make_weak_ptr(dc1), &messageData, &openTime]() {
auto dc1 = wdc1.lock(); auto dc1 = wdc1.lock();
@ -169,8 +157,8 @@ size_t benchmark(milliseconds duration) {
cout << "Goodput: " << goodput * 0.001 << " MB/s" cout << "Goodput: " << goodput * 0.001 << " MB/s"
<< " (" << goodput * 0.001 * 8 << " Mbit/s)" << endl; << " (" << goodput * 0.001 * 8 << " Mbit/s)" << endl;
pc1->close(); pc1.close();
pc2->close(); pc2.close();
rtc::Cleanup(); rtc::Cleanup();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);

View File

@ -39,7 +39,7 @@ void test_connectivity() {
// Custom MTU example // Custom MTU example
config1.mtu = 1500; config1.mtu = 1500;
auto pc1 = std::make_shared<PeerConnection>(config1); PeerConnection pc1(config1);
Configuration config2; Configuration config2;
// STUN server example (not necessary to connect locally) // STUN server example (not necessary to connect locally)
@ -51,62 +51,50 @@ void test_connectivity() {
config2.portRangeBegin = 5000; config2.portRangeBegin = 5000;
config2.portRangeEnd = 6000; config2.portRangeEnd = 6000;
auto pc2 = std::make_shared<PeerConnection>(config2); PeerConnection pc2(config2);
pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { pc1.onLocalDescription([&pc2](Description sdp) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
cout << "Description 1: " << sdp << endl; cout << "Description 1: " << sdp << endl;
pc2->setRemoteDescription(string(sdp)); pc2.setRemoteDescription(string(sdp));
}); });
pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { pc1.onLocalCandidate([&pc2](Candidate candidate) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
cout << "Candidate 1: " << candidate << endl; cout << "Candidate 1: " << candidate << endl;
pc2->addRemoteCandidate(string(candidate)); pc2.addRemoteCandidate(string(candidate));
}); });
pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; }); pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
pc1->onGatheringStateChange([](PeerConnection::GatheringState state) { pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 1: " << state << endl; cout << "Gathering state 1: " << state << endl;
}); });
pc1->onSignalingStateChange([](PeerConnection::SignalingState state) { pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
cout << "Signaling state 1: " << state << endl; cout << "Signaling state 1: " << state << endl;
}); });
pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { pc2.onLocalDescription([&pc1](Description sdp) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
cout << "Description 2: " << sdp << endl; cout << "Description 2: " << sdp << endl;
pc1->setRemoteDescription(string(sdp)); pc1.setRemoteDescription(string(sdp));
}); });
pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { pc2.onLocalCandidate([&pc1](Candidate candidate) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
cout << "Candidate 2: " << candidate << endl; cout << "Candidate 2: " << candidate << endl;
pc1->addRemoteCandidate(string(candidate)); pc1.addRemoteCandidate(string(candidate));
}); });
pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; }); pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
pc2->onGatheringStateChange([](PeerConnection::GatheringState state) { pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 2: " << state << endl; cout << "Gathering state 2: " << state << endl;
}); });
pc2->onSignalingStateChange([](PeerConnection::SignalingState state) { pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
cout << "Signaling state 2: " << state << endl; cout << "Signaling state 2: " << state << endl;
}); });
shared_ptr<DataChannel> dc2; shared_ptr<DataChannel> dc2;
pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) { pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
if (dc->label() != "test") { if (dc->label() != "test") {
cerr << "Wrong DataChannel label" << endl; cerr << "Wrong DataChannel label" << endl;
@ -124,7 +112,7 @@ void test_connectivity() {
std::atomic_store(&dc2, dc); std::atomic_store(&dc2, dc);
}); });
auto dc1 = pc1->createDataChannel("test"); auto dc1 = pc1.createDataChannel("test");
dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() { dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
auto dc1 = wdc1.lock(); auto dc1 = wdc1.lock();
if (!dc1) if (!dc1)
@ -145,35 +133,35 @@ void test_connectivity() {
while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--) while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
if (pc1->state() != PeerConnection::State::Connected && if (pc1.state() != PeerConnection::State::Connected &&
pc2->state() != PeerConnection::State::Connected) pc2.state() != PeerConnection::State::Connected)
throw runtime_error("PeerConnection is not connected"); throw runtime_error("PeerConnection is not connected");
if (!adc2 || !adc2->isOpen() || !dc1->isOpen()) if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
throw runtime_error("DataChannel is not open"); throw runtime_error("DataChannel is not open");
if (auto addr = pc1->localAddress()) if (auto addr = pc1.localAddress())
cout << "Local address 1: " << *addr << endl; cout << "Local address 1: " << *addr << endl;
if (auto addr = pc1->remoteAddress()) if (auto addr = pc1.remoteAddress())
cout << "Remote address 1: " << *addr << endl; cout << "Remote address 1: " << *addr << endl;
if (auto addr = pc2->localAddress()) if (auto addr = pc2.localAddress())
cout << "Local address 2: " << *addr << endl; cout << "Local address 2: " << *addr << endl;
if (auto addr = pc2->remoteAddress()) if (auto addr = pc2.remoteAddress())
cout << "Remote address 2: " << *addr << endl; cout << "Remote address 2: " << *addr << endl;
Candidate local, remote; Candidate local, remote;
if (pc1->getSelectedCandidatePair(&local, &remote)) { if (pc1.getSelectedCandidatePair(&local, &remote)) {
cout << "Local candidate 1: " << local << endl; cout << "Local candidate 1: " << local << endl;
cout << "Remote candidate 1: " << remote << endl; cout << "Remote candidate 1: " << remote << endl;
} }
if (pc2->getSelectedCandidatePair(&local, &remote)) { if (pc2.getSelectedCandidatePair(&local, &remote)) {
cout << "Local candidate 2: " << local << endl; cout << "Local candidate 2: " << local << endl;
cout << "Remote candidate 2: " << remote << endl; cout << "Remote candidate 2: " << remote << endl;
} }
// Try to open a second data channel with another label // Try to open a second data channel with another label
shared_ptr<DataChannel> second2; shared_ptr<DataChannel> second2;
pc2->onDataChannel([&second2](shared_ptr<DataChannel> dc) { pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
if (dc->label() != "second") { if (dc->label() != "second") {
cerr << "Wrong second DataChannel label" << endl; cerr << "Wrong second DataChannel label" << endl;
@ -191,7 +179,7 @@ void test_connectivity() {
std::atomic_store(&second2, dc); std::atomic_store(&second2, dc);
}); });
auto second1 = pc1->createDataChannel("second"); auto second1 = pc1.createDataChannel("second");
second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() { second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() {
auto second1 = wsecond1.lock(); auto second1 = wsecond1.lock();
if (!second1) if (!second1)
@ -221,8 +209,8 @@ void test_connectivity() {
DataChannelInit init; DataChannelInit init;
init.negotiated = true; init.negotiated = true;
init.id = 42; init.id = 42;
auto negotiated1 = pc1->createDataChannel("negotiated", init); auto negotiated1 = pc1.createDataChannel("negotiated", init);
auto negotiated2 = pc2->createDataChannel("negoctated", init); auto negotiated2 = pc2.createDataChannel("negoctated", init);
if (!negotiated1->isOpen() || !negotiated2->isOpen()) if (!negotiated1->isOpen() || !negotiated2->isOpen())
throw runtime_error("Negotiated DataChannel is not open"); throw runtime_error("Negotiated DataChannel is not open");
@ -246,9 +234,9 @@ void test_connectivity() {
throw runtime_error("Negotiated DataChannel failed"); throw runtime_error("Negotiated DataChannel failed");
// Delay close of peer 2 to check closing works properly // Delay close of peer 2 to check closing works properly
pc1->close(); pc1.close();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
pc2->close(); pc2.close();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
// You may call rtc::Cleanup() when finished to free static resources // You may call rtc::Cleanup() when finished to free static resources

View File

@ -36,7 +36,7 @@ void test_track() {
// STUN server example // STUN server example
// config1.iceServers.emplace_back("stun:stun.l.google.com:19302"); // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
auto pc1 = std::make_shared<PeerConnection>(config1); PeerConnection pc1(config1);
Configuration config2; Configuration config2;
// STUN server example // STUN server example
@ -45,55 +45,43 @@ void test_track() {
config2.portRangeBegin = 5000; config2.portRangeBegin = 5000;
config2.portRangeEnd = 6000; config2.portRangeEnd = 6000;
auto pc2 = std::make_shared<PeerConnection>(config2); PeerConnection pc2(config2);
pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { pc1.onLocalDescription([&pc2](Description sdp) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
cout << "Description 1: " << sdp << endl; cout << "Description 1: " << sdp << endl;
pc2->setRemoteDescription(string(sdp)); pc2.setRemoteDescription(string(sdp));
}); });
pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { pc1.onLocalCandidate([&pc2](Candidate candidate) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
cout << "Candidate 1: " << candidate << endl; cout << "Candidate 1: " << candidate << endl;
pc2->addRemoteCandidate(string(candidate)); pc2.addRemoteCandidate(string(candidate));
}); });
pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; }); pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
pc1->onGatheringStateChange([](PeerConnection::GatheringState state) { pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 1: " << state << endl; cout << "Gathering state 1: " << state << endl;
}); });
pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { pc2.onLocalDescription([&pc1](Description sdp) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
cout << "Description 2: " << sdp << endl; cout << "Description 2: " << sdp << endl;
pc1->setRemoteDescription(string(sdp)); pc1.setRemoteDescription(string(sdp));
}); });
pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { pc2.onLocalCandidate([&pc1](Candidate candidate) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
cout << "Candidate 2: " << candidate << endl; cout << "Candidate 2: " << candidate << endl;
pc1->addRemoteCandidate(string(candidate)); pc1.addRemoteCandidate(string(candidate));
}); });
pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; }); pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
pc2->onGatheringStateChange([](PeerConnection::GatheringState state) { pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 2: " << state << endl; cout << "Gathering state 2: " << state << endl;
}); });
shared_ptr<Track> t2; shared_ptr<Track> t2;
string newTrackMid; string newTrackMid;
pc2->onTrack([&t2, &newTrackMid](shared_ptr<Track> t) { pc2.onTrack([&t2, &newTrackMid](shared_ptr<Track> t) {
cout << "Track 2: Received with mid \"" << t->mid() << "\"" << endl; cout << "Track 2: Received with mid \"" << t->mid() << "\"" << endl;
if (t->mid() != newTrackMid) { if (t->mid() != newTrackMid) {
cerr << "Wrong track mid" << endl; cerr << "Wrong track mid" << endl;
@ -105,17 +93,17 @@ void test_track() {
// Test opening a track // Test opening a track
newTrackMid = "test"; newTrackMid = "test";
auto t1 = pc1->addTrack(Description::Video(newTrackMid)); auto t1 = pc1.addTrack(Description::Video(newTrackMid));
pc1->setLocalDescription(); pc1.setLocalDescription();
int attempts = 10; int attempts = 10;
shared_ptr<Track> at2; shared_ptr<Track> at2;
while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--) while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
if (pc1->state() != PeerConnection::State::Connected && if (pc1.state() != PeerConnection::State::Connected &&
pc2->state() != PeerConnection::State::Connected) pc2.state() != PeerConnection::State::Connected)
throw runtime_error("PeerConnection is not connected"); throw runtime_error("PeerConnection is not connected");
if (!at2 || !at2->isOpen() || !t1->isOpen()) if (!at2 || !at2->isOpen() || !t1->isOpen())
@ -123,9 +111,9 @@ void test_track() {
// Test renegotiation // Test renegotiation
newTrackMid = "added"; newTrackMid = "added";
t1 = pc1->addTrack(Description::Video(newTrackMid)); t1 = pc1.addTrack(Description::Video(newTrackMid));
pc1->setLocalDescription(); pc1.setLocalDescription();
attempts = 10; attempts = 10;
t2.reset(); t2.reset();
@ -138,9 +126,9 @@ void test_track() {
// TODO: Test sending RTP packets in track // TODO: Test sending RTP packets in track
// Delay close of peer 2 to check closing works properly // Delay close of peer 2 to check closing works properly
pc1->close(); pc1.close();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
pc2->close(); pc2.close();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
// You may call rtc::Cleanup() when finished to free static resources // You may call rtc::Cleanup() when finished to free static resources

View File

@ -40,7 +40,7 @@ void test_turn_connectivity() {
// Please do not use outside of libdatachannel tests // Please do not use outside of libdatachannel tests
config1.iceServers.emplace_back("turn:datachannel_test:14018314739877@stun.ageneau.net:3478"); config1.iceServers.emplace_back("turn:datachannel_test:14018314739877@stun.ageneau.net:3478");
auto pc1 = std::make_shared<PeerConnection>(config1); PeerConnection pc1(config1);
Configuration config2; Configuration config2;
// STUN server example (not necessary, just here for testing) // STUN server example (not necessary, just here for testing)
@ -50,70 +50,58 @@ void test_turn_connectivity() {
// Please do not use outside of libdatachannel tests // Please do not use outside of libdatachannel tests
config2.iceServers.emplace_back("turn:datachannel_test:14018314739877@stun.ageneau.net:3478"); config2.iceServers.emplace_back("turn:datachannel_test:14018314739877@stun.ageneau.net:3478");
auto pc2 = std::make_shared<PeerConnection>(config2); PeerConnection pc2(config2);
pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { pc1.onLocalDescription([&pc2](Description sdp) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
cout << "Description 1: " << sdp << endl; cout << "Description 1: " << sdp << endl;
pc2->setRemoteDescription(string(sdp)); pc2.setRemoteDescription(string(sdp));
}); });
pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { pc1.onLocalCandidate([&pc2](Candidate candidate) {
auto pc2 = wpc2.lock();
if (!pc2)
return;
// For this test, filter out non-relay candidates to force TURN // For this test, filter out non-relay candidates to force TURN
string str(candidate); string str(candidate);
if (str.find("relay") != string::npos) { if (str.find("relay") != string::npos) {
cout << "Candidate 1: " << str << endl; cout << "Candidate 1: " << str << endl;
pc2->addRemoteCandidate(str); pc2.addRemoteCandidate(str);
} }
}); });
pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; }); pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
pc1->onGatheringStateChange([](PeerConnection::GatheringState state) { pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 1: " << state << endl; cout << "Gathering state 1: " << state << endl;
}); });
pc1->onSignalingStateChange([](PeerConnection::SignalingState state) { pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
cout << "Signaling state 1: " << state << endl; cout << "Signaling state 1: " << state << endl;
}); });
pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { pc2.onLocalDescription([&pc1](Description sdp) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
cout << "Description 2: " << sdp << endl; cout << "Description 2: " << sdp << endl;
pc1->setRemoteDescription(string(sdp)); pc1.setRemoteDescription(string(sdp));
}); });
pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { pc2.onLocalCandidate([&pc1](Candidate candidate) {
auto pc1 = wpc1.lock();
if (!pc1)
return;
// For this test, filter out non-relay candidates to force TURN // For this test, filter out non-relay candidates to force TURN
string str(candidate); string str(candidate);
if (str.find("relay") != string::npos) { if (str.find("relay") != string::npos) {
cout << "Candidate 1: " << str << endl; cout << "Candidate 1: " << str << endl;
pc1->addRemoteCandidate(str); pc1.addRemoteCandidate(str);
} }
}); });
pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; }); pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
pc2->onGatheringStateChange([](PeerConnection::GatheringState state) { pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
cout << "Gathering state 2: " << state << endl; cout << "Gathering state 2: " << state << endl;
}); });
pc2->onSignalingStateChange([](PeerConnection::SignalingState state) { pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
cout << "Signaling state 2: " << state << endl; cout << "Signaling state 2: " << state << endl;
}); });
shared_ptr<DataChannel> dc2; shared_ptr<DataChannel> dc2;
pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) { pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
if (dc->label() != "test") { if (dc->label() != "test") {
cerr << "Wrong DataChannel label" << endl; cerr << "Wrong DataChannel label" << endl;
@ -131,7 +119,7 @@ void test_turn_connectivity() {
std::atomic_store(&dc2, dc); std::atomic_store(&dc2, dc);
}); });
auto dc1 = pc1->createDataChannel("test"); auto dc1 = pc1.createDataChannel("test");
dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() { dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
auto dc1 = wdc1.lock(); auto dc1 = wdc1.lock();
if (!dc1) if (!dc1)
@ -152,35 +140,35 @@ void test_turn_connectivity() {
while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--) while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
if (pc1->state() != PeerConnection::State::Connected && if (pc1.state() != PeerConnection::State::Connected &&
pc2->state() != PeerConnection::State::Connected) pc2.state() != PeerConnection::State::Connected)
throw runtime_error("PeerConnection is not connected"); throw runtime_error("PeerConnection is not connected");
if (!adc2 || !adc2->isOpen() || !dc1->isOpen()) if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
throw runtime_error("DataChannel is not open"); throw runtime_error("DataChannel is not open");
if (auto addr = pc1->localAddress()) if (auto addr = pc1.localAddress())
cout << "Local address 1: " << *addr << endl; cout << "Local address 1: " << *addr << endl;
if (auto addr = pc1->remoteAddress()) if (auto addr = pc1.remoteAddress())
cout << "Remote address 1: " << *addr << endl; cout << "Remote address 1: " << *addr << endl;
if (auto addr = pc2->localAddress()) if (auto addr = pc2.localAddress())
cout << "Local address 2: " << *addr << endl; cout << "Local address 2: " << *addr << endl;
if (auto addr = pc2->remoteAddress()) if (auto addr = pc2.remoteAddress())
cout << "Remote address 2: " << *addr << endl; cout << "Remote address 2: " << *addr << endl;
Candidate local, remote; Candidate local, remote;
if (pc1->getSelectedCandidatePair(&local, &remote)) { if (pc1.getSelectedCandidatePair(&local, &remote)) {
cout << "Local candidate 1: " << local << endl; cout << "Local candidate 1: " << local << endl;
cout << "Remote candidate 1: " << remote << endl; cout << "Remote candidate 1: " << remote << endl;
} }
if (pc2->getSelectedCandidatePair(&local, &remote)) { if (pc2.getSelectedCandidatePair(&local, &remote)) {
cout << "Local candidate 2: " << local << endl; cout << "Local candidate 2: " << local << endl;
cout << "Remote candidate 2: " << remote << endl; cout << "Remote candidate 2: " << remote << endl;
} }
// Try to open a second data channel with another label // Try to open a second data channel with another label
shared_ptr<DataChannel> second2; shared_ptr<DataChannel> second2;
pc2->onDataChannel([&second2](shared_ptr<DataChannel> dc) { pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
if (dc->label() != "second") { if (dc->label() != "second") {
cerr << "Wrong second DataChannel label" << endl; cerr << "Wrong second DataChannel label" << endl;
@ -198,7 +186,7 @@ void test_turn_connectivity() {
std::atomic_store(&second2, dc); std::atomic_store(&second2, dc);
}); });
auto second1 = pc1->createDataChannel("second"); auto second1 = pc1.createDataChannel("second");
second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() { second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() {
auto second1 = wsecond1.lock(); auto second1 = wsecond1.lock();
if (!second1) if (!second1)
@ -228,8 +216,8 @@ void test_turn_connectivity() {
DataChannelInit init; DataChannelInit init;
init.negotiated = true; init.negotiated = true;
init.id = 42; init.id = 42;
auto negotiated1 = pc1->createDataChannel("negotiated", init); auto negotiated1 = pc1.createDataChannel("negotiated", init);
auto negotiated2 = pc2->createDataChannel("negoctated", init); auto negotiated2 = pc2.createDataChannel("negoctated", init);
if (!negotiated1->isOpen() || !negotiated2->isOpen()) if (!negotiated1->isOpen() || !negotiated2->isOpen())
throw runtime_error("Negotiated DataChannel is not open"); throw runtime_error("Negotiated DataChannel is not open");
@ -253,9 +241,9 @@ void test_turn_connectivity() {
throw runtime_error("Negotiated DataChannel failed"); throw runtime_error("Negotiated DataChannel failed");
// Delay close of peer 2 to check closing works properly // Delay close of peer 2 to check closing works properly
pc1->close(); pc1.close();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
pc2->close(); pc2.close();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
// You may call rtc::Cleanup() when finished to free static resources // You may call rtc::Cleanup() when finished to free static resources

View File

@ -36,24 +36,20 @@ void test_websocket() {
const string myMessage = "Hello world from libdatachannel"; const string myMessage = "Hello world from libdatachannel";
auto ws = std::make_shared<WebSocket>(); WebSocket ws;
// Certificate verification can be disabled // Certificate verification can be disabled
// auto ws = std::make_shared<WebSocket>(WebSocket::Configuration{.disableTlsVerification = // WebSocket ws(WebSocket::Configuration{.disableTlsVerification = true});
// true});
ws->onOpen([wws = make_weak_ptr(ws), &myMessage]() { ws.onOpen([&ws, &myMessage]() {
auto ws = wws.lock();
if (!ws)
return;
cout << "WebSocket: Open" << endl; cout << "WebSocket: Open" << endl;
ws->send(myMessage); ws.send(myMessage);
}); });
ws->onClosed([]() { cout << "WebSocket: Closed" << endl; }); ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
std::atomic<bool> received = false; std::atomic<bool> received = false;
ws->onMessage([&received, &myMessage](variant<binary, string> message) { ws.onMessage([&received, &myMessage](variant<binary, string> message) {
if (holds_alternative<string>(message)) { if (holds_alternative<string>(message)) {
string str = std::move(get<string>(message)); string str = std::move(get<string>(message));
if ((received = (str == myMessage))) if ((received = (str == myMessage)))
@ -63,19 +59,19 @@ void test_websocket() {
} }
}); });
ws->open("wss://echo.websocket.org:443/"); ws.open("wss://echo.websocket.org:443/");
int attempts = 10; int attempts = 10;
while ((!ws->isOpen() || !received) && attempts--) while ((!ws.isOpen() || !received) && attempts--)
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
if (!ws->isOpen()) if (!ws.isOpen())
throw runtime_error("WebSocket is not open"); throw runtime_error("WebSocket is not open");
if (!received) if (!received)
throw runtime_error("Expected message not received"); throw runtime_error("Expected message not received");
ws->close(); ws.close();
this_thread::sleep_for(1s); this_thread::sleep_for(1s);
// You may call rtc::Cleanup() when finished to free static resources // You may call rtc::Cleanup() when finished to free static resources