From 0638555fefef1f32889b6a2990763d5d183c8732 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Sun, 28 Feb 2021 11:59:44 +0100 Subject: [PATCH] Removed shared_ptr to PeerConnection in tests --- examples/client/main.cpp | 15 ++++---- test/benchmark.cpp | 48 +++++++++--------------- test/connectivity.cpp | 76 ++++++++++++++++---------------------- test/track.cpp | 58 ++++++++++++----------------- test/turn_connectivity.cpp | 76 ++++++++++++++++---------------------- test/websocket.cpp | 24 +++++------- 6 files changed, 123 insertions(+), 174 deletions(-) diff --git a/examples/client/main.cpp b/examples/client/main.cpp index 243cae6..78af56c 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp @@ -54,20 +54,20 @@ shared_ptr createPeerConnection(const Configuration &config, string randomId(size_t length); int main(int argc, char **argv) try { - auto params = std::make_unique(argc, argv); + Cmdline params(argc, argv); rtc::InitLogger(LogLevel::Info); Configuration config; string stunServer = ""; - if (params->noStun()) { + if (params.noStun()) { cout << "No STUN server is configured. Only local hosts and public IP addresses supported." << endl; } else { - if (params->stunServer().substr(0, 5).compare("stun:") != 0) { + if (params.stunServer().substr(0, 5).compare("stun:") != 0) { stunServer = "stun:"; } - stunServer += params->stunServer() + ":" + to_string(params->stunPort()); + stunServer += params.stunServer() + ":" + to_string(params.stunPort()); cout << "Stun server is " << stunServer << endl; config.iceServers.emplace_back(stunServer); } @@ -129,11 +129,11 @@ int main(int argc, char **argv) try { }); string wsPrefix = ""; - if (params->webSocketServer().substr(0, 5).compare("ws://") != 0) { + if (params.webSocketServer().substr(0, 5).compare("ws://") != 0) { wsPrefix = "ws://"; } - const string url = wsPrefix + params->webSocketServer() + ":" + - to_string(params->webSocketPort()) + "/" + localId; + const string url = wsPrefix + params.webSocketServer() + ":" + + to_string(params.webSocketPort()) + "/" + localId; cout << "Url is " << url << endl; ws->open(url); @@ -251,3 +251,4 @@ string randomId(size_t length) { generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); }); return id; } + diff --git a/test/benchmark.cpp b/test/benchmark.cpp index fca21f5..0787489 100644 --- a/test/benchmark.cpp +++ b/test/benchmark.cpp @@ -42,53 +42,41 @@ size_t benchmark(milliseconds duration) { // config1.iceServers.emplace_back("stun:stun.l.google.com:19302"); // config1.mtu = 1500; - auto pc1 = std::make_shared(config1); + PeerConnection pc1(config1); Configuration config2; // config2.iceServers.emplace_back("stun:stun.l.google.com:19302"); // config2.mtu = 1500; - auto pc2 = std::make_shared(config2); + PeerConnection pc2(config2); - pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalDescription([&pc2](Description sdp) { cout << "Description 1: " << sdp << endl; - pc2->setRemoteDescription(std::move(sdp)); + pc2.setRemoteDescription(std::move(sdp)); }); - pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalCandidate([&pc2](Candidate candidate) { 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->onGatheringStateChange([](PeerConnection::GatheringState state) { + pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; }); + pc1.onGatheringStateChange([](PeerConnection::GatheringState state) { cout << "Gathering state 1: " << state << endl; }); - pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalDescription([&pc1](Description sdp) { cout << "Description 2: " << sdp << endl; - pc1->setRemoteDescription(std::move(sdp)); + pc1.setRemoteDescription(std::move(sdp)); }); - pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalCandidate([&pc1](Candidate candidate) { 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->onGatheringStateChange([](PeerConnection::GatheringState state) { + pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; }); + pc2.onGatheringStateChange([](PeerConnection::GatheringState state) { cout << "Gathering state 2: " << state << endl; }); @@ -101,7 +89,7 @@ size_t benchmark(milliseconds duration) { steady_clock::time_point startTime, openTime, receivedTime, endTime; shared_ptr dc2; - pc2->onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr dc) { + pc2.onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr dc) { dc->onMessage([&receivedTime, &receivedSize](variant message) { if (holds_alternative(message)) { const auto &bin = get(message); @@ -117,7 +105,7 @@ size_t benchmark(milliseconds duration) { }); startTime = steady_clock::now(); - auto dc1 = pc1->createDataChannel("benchmark"); + auto dc1 = pc1.createDataChannel("benchmark"); dc1->onOpen([wdc1 = make_weak_ptr(dc1), &messageData, &openTime]() { auto dc1 = wdc1.lock(); @@ -169,8 +157,8 @@ size_t benchmark(milliseconds duration) { cout << "Goodput: " << goodput * 0.001 << " MB/s" << " (" << goodput * 0.001 * 8 << " Mbit/s)" << endl; - pc1->close(); - pc2->close(); + pc1.close(); + pc2.close(); rtc::Cleanup(); this_thread::sleep_for(1s); diff --git a/test/connectivity.cpp b/test/connectivity.cpp index 882e856..67a98b2 100644 --- a/test/connectivity.cpp +++ b/test/connectivity.cpp @@ -39,7 +39,7 @@ void test_connectivity() { // Custom MTU example config1.mtu = 1500; - auto pc1 = std::make_shared(config1); + PeerConnection pc1(config1); Configuration config2; // STUN server example (not necessary to connect locally) @@ -51,62 +51,50 @@ void test_connectivity() { config2.portRangeBegin = 5000; config2.portRangeEnd = 6000; - auto pc2 = std::make_shared(config2); + PeerConnection pc2(config2); - pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalDescription([&pc2](Description sdp) { cout << "Description 1: " << sdp << endl; - pc2->setRemoteDescription(string(sdp)); + pc2.setRemoteDescription(string(sdp)); }); - pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalCandidate([&pc2](Candidate candidate) { 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; }); - pc1->onSignalingStateChange([](PeerConnection::SignalingState state) { + pc1.onSignalingStateChange([](PeerConnection::SignalingState state) { cout << "Signaling state 1: " << state << endl; }); - pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalDescription([&pc1](Description sdp) { cout << "Description 2: " << sdp << endl; - pc1->setRemoteDescription(string(sdp)); + pc1.setRemoteDescription(string(sdp)); }); - pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalCandidate([&pc1](Candidate candidate) { 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; }); - pc2->onSignalingStateChange([](PeerConnection::SignalingState state) { + pc2.onSignalingStateChange([](PeerConnection::SignalingState state) { cout << "Signaling state 2: " << state << endl; }); shared_ptr dc2; - pc2->onDataChannel([&dc2](shared_ptr dc) { + pc2.onDataChannel([&dc2](shared_ptr dc) { cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; if (dc->label() != "test") { cerr << "Wrong DataChannel label" << endl; @@ -124,7 +112,7 @@ void test_connectivity() { std::atomic_store(&dc2, dc); }); - auto dc1 = pc1->createDataChannel("test"); + auto dc1 = pc1.createDataChannel("test"); dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() { auto dc1 = wdc1.lock(); if (!dc1) @@ -145,35 +133,35 @@ void test_connectivity() { while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--) this_thread::sleep_for(1s); - if (pc1->state() != PeerConnection::State::Connected && - pc2->state() != PeerConnection::State::Connected) + if (pc1.state() != PeerConnection::State::Connected && + pc2.state() != PeerConnection::State::Connected) throw runtime_error("PeerConnection is not connected"); if (!adc2 || !adc2->isOpen() || !dc1->isOpen()) throw runtime_error("DataChannel is not open"); - if (auto addr = pc1->localAddress()) + if (auto addr = pc1.localAddress()) cout << "Local address 1: " << *addr << endl; - if (auto addr = pc1->remoteAddress()) + if (auto addr = pc1.remoteAddress()) cout << "Remote address 1: " << *addr << endl; - if (auto addr = pc2->localAddress()) + if (auto addr = pc2.localAddress()) cout << "Local address 2: " << *addr << endl; - if (auto addr = pc2->remoteAddress()) + if (auto addr = pc2.remoteAddress()) cout << "Remote address 2: " << *addr << endl; Candidate local, remote; - if (pc1->getSelectedCandidatePair(&local, &remote)) { + if (pc1.getSelectedCandidatePair(&local, &remote)) { cout << "Local candidate 1: " << local << endl; cout << "Remote candidate 1: " << remote << endl; } - if (pc2->getSelectedCandidatePair(&local, &remote)) { + if (pc2.getSelectedCandidatePair(&local, &remote)) { cout << "Local candidate 2: " << local << endl; cout << "Remote candidate 2: " << remote << endl; } // Try to open a second data channel with another label shared_ptr second2; - pc2->onDataChannel([&second2](shared_ptr dc) { + pc2.onDataChannel([&second2](shared_ptr dc) { cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; if (dc->label() != "second") { cerr << "Wrong second DataChannel label" << endl; @@ -191,7 +179,7 @@ void test_connectivity() { std::atomic_store(&second2, dc); }); - auto second1 = pc1->createDataChannel("second"); + auto second1 = pc1.createDataChannel("second"); second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() { auto second1 = wsecond1.lock(); if (!second1) @@ -221,8 +209,8 @@ void test_connectivity() { DataChannelInit init; init.negotiated = true; init.id = 42; - auto negotiated1 = pc1->createDataChannel("negotiated", init); - auto negotiated2 = pc2->createDataChannel("negoctated", init); + auto negotiated1 = pc1.createDataChannel("negotiated", init); + auto negotiated2 = pc2.createDataChannel("negoctated", init); if (!negotiated1->isOpen() || !negotiated2->isOpen()) throw runtime_error("Negotiated DataChannel is not open"); @@ -246,9 +234,9 @@ void test_connectivity() { throw runtime_error("Negotiated DataChannel failed"); // Delay close of peer 2 to check closing works properly - pc1->close(); + pc1.close(); this_thread::sleep_for(1s); - pc2->close(); + pc2.close(); this_thread::sleep_for(1s); // You may call rtc::Cleanup() when finished to free static resources diff --git a/test/track.cpp b/test/track.cpp index 9f1c7a5..372368c 100644 --- a/test/track.cpp +++ b/test/track.cpp @@ -36,7 +36,7 @@ void test_track() { // STUN server example // config1.iceServers.emplace_back("stun:stun.l.google.com:19302"); - auto pc1 = std::make_shared(config1); + PeerConnection pc1(config1); Configuration config2; // STUN server example @@ -45,55 +45,43 @@ void test_track() { config2.portRangeBegin = 5000; config2.portRangeEnd = 6000; - auto pc2 = std::make_shared(config2); + PeerConnection pc2(config2); - pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalDescription([&pc2](Description sdp) { cout << "Description 1: " << sdp << endl; - pc2->setRemoteDescription(string(sdp)); + pc2.setRemoteDescription(string(sdp)); }); - pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalCandidate([&pc2](Candidate candidate) { 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; }); - pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalDescription([&pc1](Description sdp) { cout << "Description 2: " << sdp << endl; - pc1->setRemoteDescription(string(sdp)); + pc1.setRemoteDescription(string(sdp)); }); - pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalCandidate([&pc1](Candidate candidate) { 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; }); shared_ptr t2; string newTrackMid; - pc2->onTrack([&t2, &newTrackMid](shared_ptr t) { + pc2.onTrack([&t2, &newTrackMid](shared_ptr t) { cout << "Track 2: Received with mid \"" << t->mid() << "\"" << endl; if (t->mid() != newTrackMid) { cerr << "Wrong track mid" << endl; @@ -105,17 +93,17 @@ void test_track() { // Test opening a track newTrackMid = "test"; - auto t1 = pc1->addTrack(Description::Video(newTrackMid)); + auto t1 = pc1.addTrack(Description::Video(newTrackMid)); - pc1->setLocalDescription(); + pc1.setLocalDescription(); int attempts = 10; shared_ptr at2; while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--) this_thread::sleep_for(1s); - if (pc1->state() != PeerConnection::State::Connected && - pc2->state() != PeerConnection::State::Connected) + if (pc1.state() != PeerConnection::State::Connected && + pc2.state() != PeerConnection::State::Connected) throw runtime_error("PeerConnection is not connected"); if (!at2 || !at2->isOpen() || !t1->isOpen()) @@ -123,9 +111,9 @@ void test_track() { // Test renegotiation newTrackMid = "added"; - t1 = pc1->addTrack(Description::Video(newTrackMid)); + t1 = pc1.addTrack(Description::Video(newTrackMid)); - pc1->setLocalDescription(); + pc1.setLocalDescription(); attempts = 10; t2.reset(); @@ -138,9 +126,9 @@ void test_track() { // TODO: Test sending RTP packets in track // Delay close of peer 2 to check closing works properly - pc1->close(); + pc1.close(); this_thread::sleep_for(1s); - pc2->close(); + pc2.close(); this_thread::sleep_for(1s); // You may call rtc::Cleanup() when finished to free static resources diff --git a/test/turn_connectivity.cpp b/test/turn_connectivity.cpp index e80de78..48b3969 100644 --- a/test/turn_connectivity.cpp +++ b/test/turn_connectivity.cpp @@ -40,7 +40,7 @@ void test_turn_connectivity() { // Please do not use outside of libdatachannel tests config1.iceServers.emplace_back("turn:datachannel_test:14018314739877@stun.ageneau.net:3478"); - auto pc1 = std::make_shared(config1); + PeerConnection pc1(config1); Configuration config2; // 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 config2.iceServers.emplace_back("turn:datachannel_test:14018314739877@stun.ageneau.net:3478"); - auto pc2 = std::make_shared(config2); + PeerConnection pc2(config2); - pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalDescription([&pc2](Description sdp) { cout << "Description 1: " << sdp << endl; - pc2->setRemoteDescription(string(sdp)); + pc2.setRemoteDescription(string(sdp)); }); - pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) { - auto pc2 = wpc2.lock(); - if (!pc2) - return; + pc1.onLocalCandidate([&pc2](Candidate candidate) { // For this test, filter out non-relay candidates to force TURN string str(candidate); if (str.find("relay") != string::npos) { 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; }); - pc1->onSignalingStateChange([](PeerConnection::SignalingState state) { + pc1.onSignalingStateChange([](PeerConnection::SignalingState state) { cout << "Signaling state 1: " << state << endl; }); - pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalDescription([&pc1](Description sdp) { cout << "Description 2: " << sdp << endl; - pc1->setRemoteDescription(string(sdp)); + pc1.setRemoteDescription(string(sdp)); }); - pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) { - auto pc1 = wpc1.lock(); - if (!pc1) - return; + pc2.onLocalCandidate([&pc1](Candidate candidate) { // For this test, filter out non-relay candidates to force TURN string str(candidate); if (str.find("relay") != string::npos) { 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; }); - pc2->onSignalingStateChange([](PeerConnection::SignalingState state) { + pc2.onSignalingStateChange([](PeerConnection::SignalingState state) { cout << "Signaling state 2: " << state << endl; }); shared_ptr dc2; - pc2->onDataChannel([&dc2](shared_ptr dc) { + pc2.onDataChannel([&dc2](shared_ptr dc) { cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; if (dc->label() != "test") { cerr << "Wrong DataChannel label" << endl; @@ -131,7 +119,7 @@ void test_turn_connectivity() { std::atomic_store(&dc2, dc); }); - auto dc1 = pc1->createDataChannel("test"); + auto dc1 = pc1.createDataChannel("test"); dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() { auto dc1 = wdc1.lock(); if (!dc1) @@ -152,35 +140,35 @@ void test_turn_connectivity() { while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--) this_thread::sleep_for(1s); - if (pc1->state() != PeerConnection::State::Connected && - pc2->state() != PeerConnection::State::Connected) + if (pc1.state() != PeerConnection::State::Connected && + pc2.state() != PeerConnection::State::Connected) throw runtime_error("PeerConnection is not connected"); if (!adc2 || !adc2->isOpen() || !dc1->isOpen()) throw runtime_error("DataChannel is not open"); - if (auto addr = pc1->localAddress()) + if (auto addr = pc1.localAddress()) cout << "Local address 1: " << *addr << endl; - if (auto addr = pc1->remoteAddress()) + if (auto addr = pc1.remoteAddress()) cout << "Remote address 1: " << *addr << endl; - if (auto addr = pc2->localAddress()) + if (auto addr = pc2.localAddress()) cout << "Local address 2: " << *addr << endl; - if (auto addr = pc2->remoteAddress()) + if (auto addr = pc2.remoteAddress()) cout << "Remote address 2: " << *addr << endl; Candidate local, remote; - if (pc1->getSelectedCandidatePair(&local, &remote)) { + if (pc1.getSelectedCandidatePair(&local, &remote)) { cout << "Local candidate 1: " << local << endl; cout << "Remote candidate 1: " << remote << endl; } - if (pc2->getSelectedCandidatePair(&local, &remote)) { + if (pc2.getSelectedCandidatePair(&local, &remote)) { cout << "Local candidate 2: " << local << endl; cout << "Remote candidate 2: " << remote << endl; } // Try to open a second data channel with another label shared_ptr second2; - pc2->onDataChannel([&second2](shared_ptr dc) { + pc2.onDataChannel([&second2](shared_ptr dc) { cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl; if (dc->label() != "second") { cerr << "Wrong second DataChannel label" << endl; @@ -198,7 +186,7 @@ void test_turn_connectivity() { std::atomic_store(&second2, dc); }); - auto second1 = pc1->createDataChannel("second"); + auto second1 = pc1.createDataChannel("second"); second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() { auto second1 = wsecond1.lock(); if (!second1) @@ -228,8 +216,8 @@ void test_turn_connectivity() { DataChannelInit init; init.negotiated = true; init.id = 42; - auto negotiated1 = pc1->createDataChannel("negotiated", init); - auto negotiated2 = pc2->createDataChannel("negoctated", init); + auto negotiated1 = pc1.createDataChannel("negotiated", init); + auto negotiated2 = pc2.createDataChannel("negoctated", init); if (!negotiated1->isOpen() || !negotiated2->isOpen()) throw runtime_error("Negotiated DataChannel is not open"); @@ -253,9 +241,9 @@ void test_turn_connectivity() { throw runtime_error("Negotiated DataChannel failed"); // Delay close of peer 2 to check closing works properly - pc1->close(); + pc1.close(); this_thread::sleep_for(1s); - pc2->close(); + pc2.close(); this_thread::sleep_for(1s); // You may call rtc::Cleanup() when finished to free static resources diff --git a/test/websocket.cpp b/test/websocket.cpp index 60892f7..52af229 100644 --- a/test/websocket.cpp +++ b/test/websocket.cpp @@ -36,24 +36,20 @@ void test_websocket() { const string myMessage = "Hello world from libdatachannel"; - auto ws = std::make_shared(); + WebSocket ws; // Certificate verification can be disabled - // auto ws = std::make_shared(WebSocket::Configuration{.disableTlsVerification = - // true}); + // WebSocket ws(WebSocket::Configuration{.disableTlsVerification = true}); - ws->onOpen([wws = make_weak_ptr(ws), &myMessage]() { - auto ws = wws.lock(); - if (!ws) - return; + ws.onOpen([&ws, &myMessage]() { cout << "WebSocket: Open" << endl; - ws->send(myMessage); + ws.send(myMessage); }); - ws->onClosed([]() { cout << "WebSocket: Closed" << endl; }); + ws.onClosed([]() { cout << "WebSocket: Closed" << endl; }); std::atomic received = false; - ws->onMessage([&received, &myMessage](variant message) { + ws.onMessage([&received, &myMessage](variant message) { if (holds_alternative(message)) { string str = std::move(get(message)); 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; - while ((!ws->isOpen() || !received) && attempts--) + while ((!ws.isOpen() || !received) && attempts--) this_thread::sleep_for(1s); - if (!ws->isOpen()) + if (!ws.isOpen()) throw runtime_error("WebSocket is not open"); if (!received) throw runtime_error("Expected message not received"); - ws->close(); + ws.close(); this_thread::sleep_for(1s); // You may call rtc::Cleanup() when finished to free static resources