فهرست منبع

Removed shared_ptr to PeerConnection in tests

Paul-Louis Ageneau 4 سال پیش
والد
کامیت
0638555fef
6فایلهای تغییر یافته به همراه123 افزوده شده و 174 حذف شده
  1. 8 7
      examples/client/main.cpp
  2. 18 30
      test/benchmark.cpp
  3. 32 44
      test/connectivity.cpp
  4. 23 35
      test/track.cpp
  5. 32 44
      test/turn_connectivity.cpp
  6. 10 14
      test/websocket.cpp

+ 8 - 7
examples/client/main.cpp

@@ -54,20 +54,20 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 string randomId(size_t length);
 
 int main(int argc, char **argv) try {
-	auto params = std::make_unique<Cmdline>(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;
 }
+

+ 18 - 30
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<PeerConnection>(config1);
+	PeerConnection pc1(config1);
 
 	Configuration config2;
 	// config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
 	// config2.mtu = 1500;
 
-	auto pc2 = std::make_shared<PeerConnection>(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<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) {
 			if (holds_alternative<binary>(message)) {
 				const auto &bin = get<binary>(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);

+ 32 - 44
test/connectivity.cpp

@@ -39,7 +39,7 @@ void test_connectivity() {
 	// Custom MTU example
 	config1.mtu = 1500;
 
-	auto pc1 = std::make_shared<PeerConnection>(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<PeerConnection>(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<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;
 		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<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;
 		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

+ 23 - 35
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<PeerConnection>(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<PeerConnection>(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<Track> t2;
 	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;
 		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<Track> 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

+ 32 - 44
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:[email protected]:3478");
 
-	auto pc1 = std::make_shared<PeerConnection>(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:[email protected]:3478");
 
-	auto pc2 = std::make_shared<PeerConnection>(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<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;
 		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<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;
 		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

+ 10 - 14
test/websocket.cpp

@@ -36,24 +36,20 @@ void test_websocket() {
 
 	const string myMessage = "Hello world from libdatachannel";
 
-	auto ws = std::make_shared<WebSocket>();
+	WebSocket ws;
 
 	// Certificate verification can be disabled
-	// auto ws = std::make_shared<WebSocket>(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<bool> received = false;
-	ws->onMessage([&received, &myMessage](variant<binary, string> message) {
+	ws.onMessage([&received, &myMessage](variant<binary, string> message) {
 		if (holds_alternative<string>(message)) {
 			string str = std::move(get<string>(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