Browse Source

Cleaned up tests and fixed SDP reading from console

Paul-Louis Ageneau 5 years ago
parent
commit
35d4455c4f
6 changed files with 123 additions and 171 deletions
  1. 1 1
      CMakeLists.txt
  2. 1 1
      deps/libjuice
  3. 2 0
      include/rtc/description.hpp
  4. 15 28
      test/main.cpp
  5. 51 71
      test/p2p/answerer.cpp
  6. 53 70
      test/p2p/offerer.cpp

+ 1 - 1
CMakeLists.txt

@@ -156,6 +156,6 @@ add_executable(datachannel-answerer ${TESTS_ANSWERER_SOURCES})
 set_target_properties(datachannel-answerer PROPERTIES
 	VERSION ${PROJECT_VERSION}
 	CXX_STANDARD 17)
-set_target_properties(datachannel-answerer PROPERTIES OUTPUT_NAME datachannel)
+set_target_properties(datachannel-answerer PROPERTIES OUTPUT_NAME answerer)
 target_link_libraries(datachannel-answerer datachannel)
 

+ 1 - 1
deps/libjuice

@@ -1 +1 @@
-Subproject commit 6ac73257b6797204df615c8df8200b55fb538c06
+Subproject commit 3e9d9f0aa5c1759e37766ef71ddd20dcce298618

+ 2 - 0
include/rtc/description.hpp

@@ -35,6 +35,7 @@ public:
 	enum class Role { ActPass = 0, Passive = 1, Active = 2 };
 
 	Description(const string &sdp, const string &typeString = "");
+	Description(const string &sdp, Type type);
 	Description(const string &sdp, Type type, Role role);
 
 	Type type() const;
@@ -47,6 +48,7 @@ public:
 	std::optional<size_t> maxMessageSize() const;
 	bool trickleEnabled() const;
 
+	void hintType(Type type);
 	void setFingerprint(string fingerprint);
 	void setSctpPort(uint16_t port);
 	void setMaxMessageSize(size_t size);

+ 15 - 28
test/main.cpp

@@ -35,32 +35,21 @@ template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 int main(int argc, char **argv) {
 	InitLogger(LogLevel::Warning);
 
+#ifdef _WIN32
+	WSADATA wsaData;
+	if (WSAStartup(MAKEWORD(2, 2), &wsaData))
+		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
+#endif
+
 	Configuration config;
 	// config.iceServers.emplace_back("stun:stun.l.google.com:19302");
-	// config.enableIceTcp = true;
-
-	// TURN server example
-	// IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
-	//							IceServer::RelayType::TurnUdp);
-	// config.iceServers.push_back(turnServer);
+	// config.iceServers.emplace_back(IceServer("TURN_SERVER_URL", "PORT", "USERNAME", "PASSWORD",
+	//                                         IceServer::RelayType::TurnUdp)); // libnice only
+	// config.enableIceTcp = true; // libnice only
 
 	auto pc1 = std::make_shared<PeerConnection>(config);
 	auto pc2 = std::make_shared<PeerConnection>(config);
 
-#ifdef _WIN32
-	WSADATA wsaData;
-	int iResult;
-
-	// Initialize Winsock
-	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
-	if (iResult != 0) {
-		std::string err("WSAStartup failed. Error:");
-		err.append(WSAGetLastError() + "");
-		std::cout << err;
-		return -1;
-	}
-#endif
-
 	pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](const Description &sdp) {
 		auto pc2 = wpc2.lock();
 		if (!pc2)
@@ -140,20 +129,18 @@ int main(int argc, char **argv) {
 	if (auto addr = pc2->remoteAddress())
 		cout << "Remote address 2: " << *addr << endl;
 
-	if (dc1->isOpen() && dc2->isOpen()) {
+	bool success;
+	if ((success = dc1->isOpen() && dc2->isOpen())) {
 		pc1->close();
 		pc2->close();
-
 		cout << "Success" << endl;
-#ifdef _WIN32
-		WSACleanup();
-#endif
-		return 0;
 	} else {
 		cout << "Failure" << endl;
+	}
+
 #ifdef _WIN32
 		WSACleanup();
 #endif
-		return 1;
-	}
+
+	    return success ? 0 : 1;
 }

+ 51 - 71
test/p2p/answerer.cpp

@@ -21,6 +21,7 @@
 #include <chrono>
 #include <iostream>
 #include <memory>
+#include <thread>
 
 #ifdef _WIN32
 #include <winsock2.h>
@@ -32,60 +33,46 @@ using namespace std;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 int main(int argc, char **argv) {
-	InitLogger(LogLevel::Warning);
+	InitLogger(LogLevel::Debug);
+
+#ifdef _WIN32
+	WSADATA wsaData;
+	if (WSAStartup(MAKEWORD(2, 2), &wsaData))
+		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
+#endif
 
 	Configuration config;
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
-	// config.enableIceTcp = true;
-
-	// TURN Server example
-	// IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
-	//							IceServer::RelayType::TurnUdp);
-	// config.iceServers.push_back(turnServer);
 
 	auto pc = std::make_shared<PeerConnection>(config);
 
-#ifdef _WIN32
-	WSADATA wsaData;
-	int iResult;
-
-	// Initialize Winsock
-	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
-	if (iResult != 0) {
-		std::string err("WSAStartup failed. Error:");
-		err.append(WSAGetLastError() + "");
-		std::cout << err;
-		return -1;
-	}
-#endif
-
-	pc->onLocalDescription([](const Description &sdp) {
-		std::string s(sdp);
-		std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
-		std::replace(s.begin(), s.end(), '\r', static_cast<char>(95));
-		cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
+	pc->onLocalDescription([](const Description &description) {
+		cout << "Local Description (Paste this to the other peer):" << endl;
+		cout << string(description) << endl;
 	});
 
 	pc->onLocalCandidate([](const Candidate &candidate) {
-		cout << "Local Candidate (Paste this to other peer):" << endl << candidate << endl << endl;
+		cout << "Local Candidate (Paste this to the other peer after the local description):"
+		     << endl;
+		cout << string(candidate) << endl << endl;
 	});
 
 	pc->onStateChange(
-	    [](PeerConnection::State state) { cout << "[ State: " << state << " ]" << endl; });
+	    [](PeerConnection::State state) { cout << "[State: " << state << "]" << endl; });
 	pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
-		cout << "[ Gathering State: " << state << " ]" << endl;
+		cout << "[Gathering State: " << state << "]" << endl;
 	});
 
 	shared_ptr<DataChannel> dc = nullptr;
 	pc->onDataChannel([&](shared_ptr<DataChannel> _dc) {
-		cout << "[ Got a DataChannel with label: " << _dc->label() << " ]" << endl;
+		cout << "[Got a DataChannel with label: " << _dc->label() << "]" << endl;
 		dc = _dc;
 
-		dc->onClosed([&]() { cout << "[ DataChannel closed: " << dc->label() << " ]" << endl; });
+		dc->onClosed([&]() { cout << "[DataChannel closed: " << dc->label() << "]" << endl; });
 
 		dc->onMessage([](const variant<binary, string> &message) {
 			if (holds_alternative<string>(message)) {
-				cout << "[ Received: " << get<string>(message) << " ]" << endl;
+				cout << "[Received message: " << get<string>(message) << "]" << endl;
 			}
 		});
 	});
@@ -93,68 +80,61 @@ int main(int argc, char **argv) {
 	bool exit = false;
 	while (!exit) {
 		cout << endl
+		     << "**********************************************************************************"
+		        "*****"
 		     << endl
-		     << "*************************************************************************" << endl
 		     << "* 0: Exit /"
-		     << " 1: Enter Description /"
-		     << " 2: Enter Candidate /"
-		     << " 3: Send Message *" << endl
-		     << " [Command]: ";
-
-		int command;
-		std::string sdp, candidate, message;
-		const char *a;
-		std::unique_ptr<Candidate> candidatePtr;
-		std::unique_ptr<Description> descPtr;
+		     << " 1: Enter remote description /"
+		     << " 2: Enter remote candidate /"
+		     << " 3: Send message *" << endl
+		     << "[Command]: ";
+
+		int command = -1;
 		cin >> command;
+		cin.ignore();
 
 		switch (command) {
-		case 0:
+		case 0: {
 			exit = true;
 			break;
-
-		case 1:
+		}
+		case 1: {
 			// Parse Description
-			cout << "[SDP]: ";
-			sdp = "";
-			while (sdp.length() == 0)
-				getline(cin, sdp);
-
-			std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
-			std::replace(sdp.begin(), sdp.end(), static_cast<char>(95), '\r');
-			descPtr = std::make_unique<Description>(sdp, Description::Type::Offer,
-			                                        Description::Role::Passive);
-			pc->setRemoteDescription(*descPtr);
+			cout << "[Description]: ";
+			string sdp, line;
+			while (getline(cin, line) && !line.empty()) {
+				sdp += line;
+				sdp += "\r\n";
+			}
+			std::cout << sdp;
+			pc->setRemoteDescription(sdp);
 			break;
-
-		case 2:
+		}
+		case 2: {
 			// Parse Candidate
 			cout << "[Candidate]: ";
-			candidate = "";
-			while (candidate.length() == 0)
-				getline(cin, candidate);
-
-			candidatePtr = std::make_unique<Candidate>(candidate);
-			pc->addRemoteCandidate(*candidatePtr);
+			string candidate;
+			getline(cin, candidate);
+			pc->addRemoteCandidate(candidate);
 			break;
-
-		case 3:
+		}
+		case 3: {
 			// Send Message
 			if (!dc || !dc->isOpen()) {
 				cout << "** Channel is not Open ** ";
 				break;
 			}
 			cout << "[Message]: ";
-			message = "";
-			while (message.length() == 0)
-				getline(cin, message);
+			string message;
+			getline(cin, message);
 			dc->send(message);
 			break;
-
-		default:
+		}
+		default: {
 			cout << "** Invalid Command ** ";
 			break;
 		}
+		}
 	}
 
 	if (dc)

+ 53 - 70
test/p2p/offerer.cpp

@@ -21,6 +21,7 @@
 #include <chrono>
 #include <iostream>
 #include <memory>
+#include <thread>
 
 #ifdef _WIN32
 #include <winsock2.h>
@@ -34,124 +35,106 @@ template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 int main(int argc, char **argv) {
 	InitLogger(LogLevel::Warning);
 
+#ifdef _WIN32
+	WSADATA wsaData;
+	if (WSAStartup(MAKEWORD(2, 2), &wsaData))
+		throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
+#endif
+
 	Configuration config;
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
-	// config.enableIceTcp = true;
-
-	// TURN server example
-	// IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
-	//							IceServer::RelayType::TurnUdp);
-	// config.iceServers.push_back(turnServer);
 
 	auto pc = std::make_shared<PeerConnection>(config);
 
-#ifdef _WIN32
-	WSADATA wsaData;
-	int iResult;
-
-	// Initialize Winsock
-	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
-	if (iResult != 0) {
-		std::string err("WSAStartup failed. Error:");
-		err.append(WSAGetLastError() + "");
-		std::cout << err;
-		return -1;
-	}
-#endif
-
-	pc->onLocalDescription([](const Description &sdp) {
-		std::string s(sdp);
-		std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
-		std::replace(s.begin(), s.end(), '\r', static_cast<char>(95));
-		cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
+	pc->onLocalDescription([](const Description &description) {
+		cout << "Local Description (Paste this to the other peer):" << endl;
+		cout << string(description) << endl;
 	});
 
 	pc->onLocalCandidate([](const Candidate &candidate) {
-		cout << "Local Candidate (Paste this to other peer):" << endl << candidate << endl << endl;
+		cout << "Local Candidate (Paste this to the other peer after the local description):"
+		     << endl;
+		cout << string(candidate) << endl << endl;
 	});
 
 	pc->onStateChange(
-	    [](PeerConnection::State state) { cout << "[ State: " << state << " ]" << endl; });
+	    [](PeerConnection::State state) { cout << "[State: " << state << "]" << endl; });
 
 	pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
-		cout << "[ Gathering State: " << state << " ]" << endl;
+		cout << "[Gathering State: " << state << "]" << endl;
 	});
 
-	auto dc = pc->createDataChannel("test");
-	dc->onOpen([&]() { cout << "[ DataChannel open: " << dc->label() << " ]" << endl; });
+	auto dc = pc->createDataChannel("test"); // this is the offerer, so create a data channel
+
+	dc->onOpen([&]() { cout << "[DataChannel open: " << dc->label() << "]" << endl; });
 
-	dc->onClosed([&]() { cout << "[ DataChannel closed: " << dc->label() << " ]" << endl; });
+	dc->onClosed([&]() { cout << "[DataChannel closed: " << dc->label() << "]" << endl; });
 
 	dc->onMessage([](const variant<binary, string> &message) {
 		if (holds_alternative<string>(message)) {
-			cout << "[ Received: " << get<string>(message) << " ]" << endl;
+			cout << "[Received: " << get<string>(message) << "]" << endl;
 		}
 	});
 
+	this_thread::sleep_for(1s);
+
 	bool exit = false;
 	while (!exit) {
 		cout << endl
+		     << "**********************************************************************************"
+		        "*****"
 		     << endl
-		     << "*************************************************************************" << endl
 		     << "* 0: Exit /"
-		     << " 1: Enter Description /"
-		     << " 2: Enter Candidate /"
-		     << " 3: Send Message *" << endl
-		     << " [Command]: ";
-
-		int command;
-		std::string sdp, candidate, message;
-		const char *a;
-		std::unique_ptr<Candidate> candidatePtr;
-		std::unique_ptr<Description> descPtr;
+		     << " 1: Enter remote description /"
+		     << " 2: Enter remote candidate /"
+		     << " 3: Send message *" << endl
+		     << "[Command]: ";
+
+		int command = -1;
 		cin >> command;
+		cin.ignore();
 
 		switch (command) {
-		case 0:
+		case 0: {
 			exit = true;
 			break;
-
-		case 1:
+		}
+		case 1: {
 			// Parse Description
-			cout << "[SDP]: ";
-			sdp = "";
-			while (sdp.length() == 0)
-				getline(cin, sdp);
-
-			std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
-			std::replace(sdp.begin(), sdp.end(), static_cast<char>(95), '\r');
-			descPtr = std::make_unique<Description>(sdp);
-			pc->setRemoteDescription(*descPtr);
+			cout << "[Description]: ";
+			string sdp, line;
+			while (getline(cin, line) && !line.empty()) {
+				sdp += line;
+				sdp += "\r\n";
+			}
+			pc->setRemoteDescription(sdp);
 			break;
-
-		case 2:
+		}
+		case 2: {
 			// Parse Candidate
 			cout << "[Candidate]: ";
-			candidate = "";
-			while (candidate.length() == 0)
-				getline(cin, candidate);
-
-			candidatePtr = std::make_unique<Candidate>(candidate);
-			pc->addRemoteCandidate(*candidatePtr);
+			string candidate;
+			getline(cin, candidate);
+			pc->addRemoteCandidate(candidate);
 			break;
-
-		case 3:
+		}
+		case 3: {
 			// Send Message
 			if (!dc->isOpen()) {
 				cout << "** Channel is not Open ** ";
 				break;
 			}
 			cout << "[Message]: ";
-			message = "";
-			while (message.length() == 0)
-				getline(cin, message);
+			string message;
+			getline(cin, message);
 			dc->send(message);
 			break;
-
-		default:
+		}
+		default: {
 			cout << "** Invalid Command ** ";
 			break;
 		}
+		}
 	}
 
 	if (dc)