فهرست منبع

Examples: Cleanup

Paul-Louis Ageneau 3 سال پیش
والد
کامیت
dbe9bbda7d
6فایلهای تغییر یافته به همراه349 افزوده شده و 322 حذف شده
  1. 116 111
      examples/client-benchmark/main.cpp
  2. 100 88
      examples/client/main.cpp
  3. 64 58
      examples/copy-paste/answerer.cpp
  4. 62 57
      examples/copy-paste/offerer.cpp
  5. 4 5
      examples/media/main.cpp
  6. 3 3
      examples/sfu-media/main.cpp

+ 116 - 111
examples/client-benchmark/main.cpp

@@ -39,31 +39,28 @@
 #include <thread>
 #include <thread>
 #include <unordered_map>
 #include <unordered_map>
 
 
-using namespace rtc;
-using namespace std;
 using namespace std::chrono_literals;
 using namespace std::chrono_literals;
-
-using chrono::milliseconds;
-using chrono::steady_clock;
-
-using json = nlohmann::json;
-
+using std::shared_ptr;
+using std::weak_ptr;
+using std::chrono::milliseconds;
+using std::chrono::steady_clock;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 
-unordered_map<string, shared_ptr<PeerConnection>> peerConnectionMap;
-unordered_map<string, shared_ptr<DataChannel>> dataChannelMap;
+using nlohmann::json;
 
 
-string localId;
+std::string localId;
+std::unordered_map<std::string, shared_ptr<rtc::PeerConnection>> peerConnectionMap;
+std::unordered_map<std::string, shared_ptr<rtc::DataChannel>> dataChannelMap;
 
 
-shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
-                                                weak_ptr<WebSocket> wws, string id);
-string randomId(size_t length);
+shared_ptr<rtc::PeerConnection> createPeerConnection(const rtc::Configuration &config,
+                                                     weak_ptr<rtc::WebSocket> wws, std::string id);
+std::string randomId(size_t length);
 
 
 // Benchmark
 // Benchmark
 const size_t messageSize = 65535;
 const size_t messageSize = 65535;
-binary messageData(messageSize);
-unordered_map<string, atomic<size_t>> receivedSizeMap;
-unordered_map<string, atomic<size_t>> sentSizeMap;
+rtc::binary messageData(messageSize);
+std::unordered_map<std::string, std::atomic<size_t>> receivedSizeMap;
+std::unordered_map<std::string, std::atomic<size_t>> sentSizeMap;
 bool noSend = false;
 bool noSend = false;
 
 
 // Benchmark - enableThroughputSet params
 // Benchmark - enableThroughputSet params
@@ -76,7 +73,7 @@ const int stepDurationInMs = int(1000 / STEP_COUNT_FOR_1_SEC);
 int main(int argc, char **argv) try {
 int main(int argc, char **argv) try {
 	Cmdline params(argc, argv);
 	Cmdline params(argc, argv);
 
 
-	rtc::InitLogger(LogLevel::Info);
+	rtc::InitLogger(rtc::LogLevel::Info);
 
 
 	// Benchmark - construct message to send
 	// Benchmark - construct message to send
 	fill(messageData.begin(), messageData.end(), std::byte(0xFF));
 	fill(messageData.begin(), messageData.end(), std::byte(0xFF));
@@ -89,110 +86,113 @@ int main(int argc, char **argv) try {
 	// No Send option
 	// No Send option
 	noSend = params.noSend();
 	noSend = params.noSend();
 	if (noSend)
 	if (noSend)
-		cout << "Not Sending data. (One way benchmark)." << endl;
+		std::cout << "Not sending data (one way benchmark)." << std::endl;
 
 
-	Configuration config;
-	string stunServer = "";
+	rtc::Configuration config;
+	std::string stunServer = "";
 	if (params.noStun()) {
 	if (params.noStun()) {
-		cout << "No STUN server is configured. Only local hosts and public IP addresses supported."
-		     << endl;
+		std::cout
+		    << "No STUN server is configured. Only local hosts and public IP addresses supported."
+		    << std::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());
-		cout << "Stun server is " << stunServer << endl;
+		stunServer += params.stunServer() + ":" + std::to_string(params.stunPort());
+		std::cout << "STUN server is " << stunServer << std::endl;
 		config.iceServers.emplace_back(stunServer);
 		config.iceServers.emplace_back(stunServer);
 	}
 	}
 
 
 	localId = randomId(4);
 	localId = randomId(4);
-	cout << "The local ID is: " << localId << endl;
+	std::cout << "The local ID is " << localId << std::endl;
 
 
-	auto ws = make_shared<WebSocket>();
+	auto ws = std::make_shared<rtc::WebSocket>();
 
 
 	std::promise<void> wsPromise;
 	std::promise<void> wsPromise;
 	auto wsFuture = wsPromise.get_future();
 	auto wsFuture = wsPromise.get_future();
 
 
 	ws->onOpen([&wsPromise]() {
 	ws->onOpen([&wsPromise]() {
-		cout << "WebSocket connected, signaling ready" << endl;
+		std::cout << "WebSocket connected, signaling ready" << std::endl;
 		wsPromise.set_value();
 		wsPromise.set_value();
 	});
 	});
 
 
-	ws->onError([&wsPromise](string s) {
-		cout << "WebSocket error" << endl;
+	ws->onError([&wsPromise](std::string s) {
+		std::cout << "WebSocket error" << std::endl;
 		wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s)));
 		wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s)));
 	});
 	});
 
 
-	ws->onClosed([]() { cout << "WebSocket closed" << endl; });
+	ws->onClosed([]() { std::cout << "WebSocket closed" << std::endl; });
 
 
-	ws->onMessage([&](variant<binary, string> data) {
-		if (!holds_alternative<string>(data))
+	ws->onMessage([&config, wws = make_weak_ptr(ws)](auto data) {
+		if (!std::holds_alternative<std::string>(data))
 			return;
 			return;
 
 
-		json message = json::parse(get<string>(data));
+		json message = json::parse(std::get<std::string>(data));
 
 
 		auto it = message.find("id");
 		auto it = message.find("id");
 		if (it == message.end())
 		if (it == message.end())
 			return;
 			return;
-		string id = it->get<string>();
+
+		auto id = it->get<std::string>();
 
 
 		it = message.find("type");
 		it = message.find("type");
 		if (it == message.end())
 		if (it == message.end())
 			return;
 			return;
-		string type = it->get<string>();
 
 
-		shared_ptr<PeerConnection> pc;
+		auto type = it->get<std::string>();
+
+		shared_ptr<rtc::PeerConnection> pc;
 		if (auto jt = peerConnectionMap.find(id); jt != peerConnectionMap.end()) {
 		if (auto jt = peerConnectionMap.find(id); jt != peerConnectionMap.end()) {
 			pc = jt->second;
 			pc = jt->second;
 		} else if (type == "offer") {
 		} else if (type == "offer") {
-			cout << "Answering to " + id << endl;
-			pc = createPeerConnection(config, ws, id);
+			std::cout << "Answering to " + id << std::endl;
+			pc = createPeerConnection(config, wws, id);
 		} else {
 		} else {
 			return;
 			return;
 		}
 		}
 
 
 		if (type == "offer" || type == "answer") {
 		if (type == "offer" || type == "answer") {
-			auto sdp = message["description"].get<string>();
-			pc->setRemoteDescription(Description(sdp, type));
+			auto sdp = message["description"].get<std::string>();
+			pc->setRemoteDescription(rtc::Description(sdp, type));
 		} else if (type == "candidate") {
 		} else if (type == "candidate") {
-			auto sdp = message["candidate"].get<string>();
-			auto mid = message["mid"].get<string>();
-			pc->addRemoteCandidate(Candidate(sdp, mid));
+			auto sdp = message["candidate"].get<std::string>();
+			auto mid = message["mid"].get<std::string>();
+			pc->addRemoteCandidate(rtc::Candidate(sdp, mid));
 		}
 		}
 	});
 	});
 
 
-	string wsPrefix = "";
-	if (params.webSocketServer().substr(0, 5).compare("ws://") != 0) {
-		wsPrefix = "ws://";
-	}
-	const string url = wsPrefix + params.webSocketServer() + ":" +
-	                   to_string(params.webSocketPort()) + "/" + localId;
-	cout << "Url is " << url << endl;
+	const std::string wsPrefix =
+	    params.webSocketServer().find("://") == std::string::npos ? "ws://" : "";
+	const std::string url = wsPrefix + params.webSocketServer() + ":" +
+	                        std::to_string(params.webSocketPort()) + "/" + localId;
+
+	std::cout << "WebSocket URL is " << url << std::endl;
 	ws->open(url);
 	ws->open(url);
 
 
-	cout << "Waiting for signaling to be connected..." << endl;
+	std::cout << "Waiting for signaling to be connected..." << std::endl;
 	wsFuture.get();
 	wsFuture.get();
 
 
-	string id;
-	cout << "Enter a remote ID to send an offer:" << endl;
-	cin >> id;
-	cin.ignore();
+	std::string id;
+	std::cout << "Enter a remote ID to send an offer:" << std::endl;
+	std::cin >> id;
+	std::cin.ignore();
 	if (id.empty()) {
 	if (id.empty()) {
 		// Nothing to do
 		// Nothing to do
 		return 0;
 		return 0;
 	}
 	}
+
 	if (id == localId) {
 	if (id == localId) {
-		cout << "Invalid remote ID (This is my local ID). Exiting..." << endl;
+		std::cout << "Invalid remote ID (This is the local ID). Exiting..." << std::endl;
 		return 0;
 		return 0;
 	}
 	}
 
 
-	cout << "Offering to " + id << endl;
+	std::cout << "Offering to " + id << std::endl;
 	auto pc = createPeerConnection(config, ws, id);
 	auto pc = createPeerConnection(config, ws, id);
 
 
 	// We are the offerer, so create a data channel to initiate the process
 	// We are the offerer, so create a data channel to initiate the process
 	for (int i = 1; i <= params.dataChannelCount(); i++) {
 	for (int i = 1; i <= params.dataChannelCount(); i++) {
-		const string label = "DC-" + std::to_string(i);
-		cout << "Creating DataChannel with label \"" << label << "\"" << endl;
+		const std::string label = "DC-" + std::to_string(i);
+		std::cout << "Creating DataChannel with label \"" << label << "\"" << std::endl;
 		auto dc = pc->createDataChannel(label);
 		auto dc = pc->createDataChannel(label);
 		receivedSizeMap.emplace(label, 0);
 		receivedSizeMap.emplace(label, 0);
 		sentSizeMap.emplace(label, 0);
 		sentSizeMap.emplace(label, 0);
@@ -201,7 +201,7 @@ int main(int argc, char **argv) try {
 		dc->setBufferedAmountLowThreshold(bufferSize);
 		dc->setBufferedAmountLowThreshold(bufferSize);
 
 
 		dc->onOpen([id, wdc = make_weak_ptr(dc), label]() {
 		dc->onOpen([id, wdc = make_weak_ptr(dc), label]() {
-			cout << "DataChannel from " << id << " open" << endl;
+			std::cout << "DataChannel from " << id << " open" << std::endl;
 			if (noSend)
 			if (noSend)
 				return;
 				return;
 
 
@@ -242,18 +242,18 @@ int main(int argc, char **argv) try {
 			}
 			}
 		});
 		});
 
 
-		dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; });
+		dc->onClosed([id]() { std::cout << "DataChannel from " << id << " closed" << std::endl; });
 
 
-		dc->onMessage([id, wdc = make_weak_ptr(dc), label](variant<binary, string> data) {
-			if (holds_alternative<binary>(data))
-				receivedSizeMap.at(label) += get<binary>(data).size();
+		dc->onMessage([id, wdc = make_weak_ptr(dc), label](auto data) {
+			if (std::holds_alternative<rtc::binary>(data))
+				receivedSizeMap.at(label) += std::get<rtc::binary>(data).size();
 		});
 		});
 
 
 		dataChannelMap.emplace(label, dc);
 		dataChannelMap.emplace(label, dc);
 	}
 	}
 
 
 	const int duration = params.durationInSec() > 0 ? params.durationInSec() : INT32_MAX;
 	const int duration = params.durationInSec() > 0 ? params.durationInSec() : INT32_MAX;
-	cout << "Benchmark will run for " << duration << " seconds" << endl;
+	std::cout << "Benchmark will run for " << duration << " seconds" << std::endl;
 
 
 	int printCounter = 0;
 	int printCounter = 0;
 	int printStatCounter = 0;
 	int printStatCounter = 0;
@@ -262,7 +262,7 @@ int main(int argc, char **argv) try {
 	// Byte count to send for every loop
 	// Byte count to send for every loop
 	int byteToSendOnEveryLoop = throughtputSetAsKB * stepDurationInMs;
 	int byteToSendOnEveryLoop = throughtputSetAsKB * stepDurationInMs;
 	for (int i = 1; i <= duration * STEP_COUNT_FOR_1_SEC; ++i) {
 	for (int i = 1; i <= duration * STEP_COUNT_FOR_1_SEC; ++i) {
-		this_thread::sleep_for(milliseconds(stepDurationInMs));
+		std::this_thread::sleep_for(milliseconds(stepDurationInMs));
 		printCounter++;
 		printCounter++;
 
 
 		if (enableThroughputSet) {
 		if (enableThroughputSet) {
@@ -274,7 +274,7 @@ int main(int argc, char **argv) try {
 			int byteToSendThisLoop = static_cast<int>(
 			int byteToSendThisLoop = static_cast<int>(
 			    byteToSendOnEveryLoop * ((elapsedTimeInSecs * 1000.0) / stepDurationInMs));
 			    byteToSendOnEveryLoop * ((elapsedTimeInSecs * 1000.0) / stepDurationInMs));
 
 
-			binary tempMessageData(byteToSendThisLoop);
+			rtc::binary tempMessageData(byteToSendThisLoop);
 			fill(tempMessageData.begin(), tempMessageData.end(), std::byte(0xFF));
 			fill(tempMessageData.begin(), tempMessageData.end(), std::byte(0xFF));
 
 
 			for (const auto &[label, dc] : dataChannelMap) {
 			for (const auto &[label, dc] : dataChannelMap) {
@@ -292,39 +292,40 @@ int main(int argc, char **argv) try {
 
 
 			unsigned long receiveSpeedTotal = 0;
 			unsigned long receiveSpeedTotal = 0;
 			unsigned long sendSpeedTotal = 0;
 			unsigned long sendSpeedTotal = 0;
-			cout << "#" << i / STEP_COUNT_FOR_1_SEC << endl;
+			std::cout << "#" << i / STEP_COUNT_FOR_1_SEC << std::endl;
 			for (const auto &[label, dc] : dataChannelMap) {
 			for (const auto &[label, dc] : dataChannelMap) {
 				unsigned long channelReceiveSpeed = static_cast<int>(
 				unsigned long channelReceiveSpeed = static_cast<int>(
 				    receivedSizeMap[label].exchange(0) / (elapsedTimeInSecs * 1000));
 				    receivedSizeMap[label].exchange(0) / (elapsedTimeInSecs * 1000));
 				unsigned long channelSendSpeed =
 				unsigned long channelSendSpeed =
 				    static_cast<int>(sentSizeMap[label].exchange(0) / (elapsedTimeInSecs * 1000));
 				    static_cast<int>(sentSizeMap[label].exchange(0) / (elapsedTimeInSecs * 1000));
 
 
-				cout << std::setw(10) << label << " Received: " << channelReceiveSpeed << " KB/s"
-				     << "   Sent: " << channelSendSpeed << " KB/s"
-				     << "   BufferSize: " << dc->bufferedAmount() << endl;
+				std::cout << std::setw(10) << label << " Received: " << channelReceiveSpeed
+				          << " KB/s"
+				          << "   Sent: " << channelSendSpeed << " KB/s"
+				          << "   BufferSize: " << dc->bufferedAmount() << std::endl;
 
 
 				receiveSpeedTotal += channelReceiveSpeed;
 				receiveSpeedTotal += channelReceiveSpeed;
 				sendSpeedTotal += channelSendSpeed;
 				sendSpeedTotal += channelSendSpeed;
 			}
 			}
-			cout << std::setw(10) << "TOTL"
-			     << " Received: " << receiveSpeedTotal << " KB/s"
-			     << "   Sent: " << sendSpeedTotal << " KB/s" << endl;
+			std::cout << std::setw(10) << "TOTAL"
+			          << " Received: " << receiveSpeedTotal << " KB/s"
+			          << "   Sent: " << sendSpeedTotal << " KB/s" << std::endl;
 
 
 			printStatCounter++;
 			printStatCounter++;
 			printCounter = 0;
 			printCounter = 0;
 		}
 		}
 
 
 		if (printStatCounter >= 5) {
 		if (printStatCounter >= 5) {
-			cout << "Stats# "
-			     << "Received Total: " << pc->bytesReceived() / (1000 * 1000) << " MB"
-			     << "   Sent Total: " << pc->bytesSent() / (1000 * 1000) << " MB"
-			     << "   RTT: " << pc->rtt().value_or(0ms).count() << " ms" << endl;
-			cout << endl;
+			std::cout << "Stats# "
+			          << "Received Total: " << pc->bytesReceived() / (1000 * 1000) << " MB"
+			          << "   Sent Total: " << pc->bytesSent() / (1000 * 1000) << " MB"
+			          << "   RTT: " << pc->rtt().value_or(0ms).count() << " ms" << std::endl;
+			std::cout << std::endl;
 			printStatCounter = 0;
 			printStatCounter = 0;
 		}
 		}
 	}
 	}
 
 
-	cout << "Cleaning up..." << endl;
+	std::cout << "Cleaning up..." << std::endl;
 
 
 	dataChannelMap.clear();
 	dataChannelMap.clear();
 	peerConnectionMap.clear();
 	peerConnectionMap.clear();
@@ -342,40 +343,44 @@ int main(int argc, char **argv) try {
 }
 }
 
 
 // Create and setup a PeerConnection
 // Create and setup a PeerConnection
-shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
-                                                weak_ptr<WebSocket> wws, string id) {
-	auto pc = make_shared<PeerConnection>(config);
+shared_ptr<rtc::PeerConnection> createPeerConnection(const rtc::Configuration &config,
+                                                     weak_ptr<rtc::WebSocket> wws, std::string id) {
+	auto pc = std::make_shared<rtc::PeerConnection>(config);
 
 
-	pc->onStateChange([](PeerConnection::State state) { cout << "State: " << state << endl; });
+	pc->onStateChange(
+	    [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
 
 
-	pc->onGatheringStateChange(
-	    [](PeerConnection::GatheringState state) { cout << "Gathering State: " << state << endl; });
+	pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) {
+		std::cout << "Gathering State: " << state << std::endl;
+	});
 
 
-	pc->onLocalDescription([wws, id](Description description) {
-		json message = {
-		    {"id", id}, {"type", description.typeString()}, {"description", string(description)}};
+	pc->onLocalDescription([wws, id](rtc::Description description) {
+		json message = {{"id", id},
+		                {"type", description.typeString()},
+		                {"description", std::string(description)}};
 
 
 		if (auto ws = wws.lock())
 		if (auto ws = wws.lock())
 			ws->send(message.dump());
 			ws->send(message.dump());
 	});
 	});
 
 
-	pc->onLocalCandidate([wws, id](Candidate candidate) {
+	pc->onLocalCandidate([wws, id](rtc::Candidate candidate) {
 		json message = {{"id", id},
 		json message = {{"id", id},
 		                {"type", "candidate"},
 		                {"type", "candidate"},
-		                {"candidate", string(candidate)},
+		                {"candidate", std::string(candidate)},
 		                {"mid", candidate.mid()}};
 		                {"mid", candidate.mid()}};
 
 
 		if (auto ws = wws.lock())
 		if (auto ws = wws.lock())
 			ws->send(message.dump());
 			ws->send(message.dump());
 	});
 	});
 
 
-	pc->onDataChannel([id](shared_ptr<DataChannel> dc) {
-		const string label = dc->label();
-		cout << "DataChannel from " << id << " received with label \"" << label << "\"" << endl;
+	pc->onDataChannel([id](shared_ptr<rtc::DataChannel> dc) {
+		const std::string label = dc->label();
+		std::cout << "DataChannel from " << id << " received with label \"" << label << "\""
+		          << std::endl;
 
 
-		cout << "###########################################" << endl;
-		cout << "### Check other peer's screen for stats ###" << endl;
-		cout << "###########################################" << endl;
+		std::cout << "###########################################" << std::endl;
+		std::cout << "### Check other peer's screen for stats ###" << std::endl;
+		std::cout << "###########################################" << std::endl;
 
 
 		receivedSizeMap.emplace(dc->label(), 0);
 		receivedSizeMap.emplace(dc->label(), 0);
 		sentSizeMap.emplace(dc->label(), 0);
 		sentSizeMap.emplace(dc->label(), 0);
@@ -402,7 +407,7 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 				// Byte count to send for every loop
 				// Byte count to send for every loop
 				int byteToSendOnEveryLoop = throughtputSetAsKB * stepDurationInMs;
 				int byteToSendOnEveryLoop = throughtputSetAsKB * stepDurationInMs;
 				while (true) {
 				while (true) {
-					this_thread::sleep_for(milliseconds(stepDurationInMs));
+					std::this_thread::sleep_for(milliseconds(stepDurationInMs));
 
 
 					auto dcLocked = wdc.lock();
 					auto dcLocked = wdc.lock();
 					if (!dcLocked)
 					if (!dcLocked)
@@ -421,7 +426,7 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 						    static_cast<int>(byteToSendOnEveryLoop *
 						    static_cast<int>(byteToSendOnEveryLoop *
 						                     ((elapsedTimeInSecs * 1000.0) / stepDurationInMs));
 						                     ((elapsedTimeInSecs * 1000.0) / stepDurationInMs));
 
 
-						binary tempMessageData(byteToSendThisLoop);
+						rtc::binary tempMessageData(byteToSendThisLoop);
 						fill(tempMessageData.begin(), tempMessageData.end(), std::byte(0xFF));
 						fill(tempMessageData.begin(), tempMessageData.end(), std::byte(0xFF));
 
 
 						if (dcLocked->bufferedAmount() <= bufferSize) {
 						if (dcLocked->bufferedAmount() <= bufferSize) {
@@ -432,7 +437,7 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 						std::cout << "Send failed: " << e.what() << std::endl;
 						std::cout << "Send failed: " << e.what() << std::endl;
 					}
 					}
 				}
 				}
-				cout << "Send Data Thread exiting..." << endl;
+				std::cout << "Send Data Thread exiting..." << std::endl;
 			}).detach();
 			}).detach();
 		}
 		}
 
 
@@ -458,11 +463,11 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 			}
 			}
 		});
 		});
 
 
-		dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; });
+		dc->onClosed([id]() { std::cout << "DataChannel from " << id << " closed" << std::endl; });
 
 
-		dc->onMessage([id, wdc = make_weak_ptr(dc), label](variant<binary, string> data) {
-			if (holds_alternative<binary>(data))
-				receivedSizeMap.at(label) += get<binary>(data).size();
+		dc->onMessage([id, wdc = make_weak_ptr(dc), label](auto data) {
+			if (std::holds_alternative<rtc::binary>(data))
+				receivedSizeMap.at(label) += std::get<rtc::binary>(data).size();
 		});
 		});
 
 
 		dataChannelMap.emplace(label, dc);
 		dataChannelMap.emplace(label, dc);
@@ -473,12 +478,12 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 };
 };
 
 
 // Helper function to generate a random ID
 // Helper function to generate a random ID
-string randomId(size_t length) {
-	static const string characters(
+std::string randomId(size_t length) {
+	static const std::string characters(
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
-	string id(length, '0');
-	default_random_engine rng(random_device{}());
-	uniform_int_distribution<int> dist(0, int(characters.size() - 1));
-	generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); });
+	std::string id(length, '0');
+	std::default_random_engine rng(std::random_device{}());
+	std::uniform_int_distribution<int> dist(0, int(characters.size() - 1));
+	std::generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); });
 	return id;
 	return id;
 }
 }

+ 100 - 88
examples/client/main.cpp

@@ -36,153 +36,160 @@
 #include <thread>
 #include <thread>
 #include <unordered_map>
 #include <unordered_map>
 
 
-using namespace rtc;
-using namespace std;
 using namespace std::chrono_literals;
 using namespace std::chrono_literals;
-
-using json = nlohmann::json;
-
+using std::shared_ptr;
+using std::weak_ptr;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 
-unordered_map<string, shared_ptr<PeerConnection>> peerConnectionMap;
-unordered_map<string, shared_ptr<DataChannel>> dataChannelMap;
+using nlohmann::json;
 
 
-string localId;
+std::string localId;
+std::unordered_map<std::string, shared_ptr<rtc::PeerConnection>> peerConnectionMap;
+std::unordered_map<std::string, shared_ptr<rtc::DataChannel>> dataChannelMap;
 
 
-shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
-                                                weak_ptr<WebSocket> wws, string id);
-string randomId(size_t length);
+shared_ptr<rtc::PeerConnection> createPeerConnection(const rtc::Configuration &config,
+                                                     weak_ptr<rtc::WebSocket> wws, std::string id);
+std::string randomId(size_t length);
 
 
 int main(int argc, char **argv) try {
 int main(int argc, char **argv) try {
 	Cmdline params(argc, argv);
 	Cmdline params(argc, argv);
 
 
-	rtc::InitLogger(LogLevel::Info);
+	rtc::InitLogger(rtc::LogLevel::Info);
 
 
-	Configuration config;
-	string stunServer = "";
+	rtc::Configuration config;
+	std::string stunServer = "";
 	if (params.noStun()) {
 	if (params.noStun()) {
-		cout << "No STUN server is configured. Only local hosts and public IP addresses supported."
-		     << endl;
+		std::cout
+		    << "No STUN server is configured. Only local hosts and public IP addresses supported."
+		    << std::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());
-		cout << "Stun server is " << stunServer << endl;
+		stunServer += params.stunServer() + ":" + std::to_string(params.stunPort());
+		std::cout << "STUN server is " << stunServer << std::endl;
 		config.iceServers.emplace_back(stunServer);
 		config.iceServers.emplace_back(stunServer);
 	}
 	}
 
 
 	if (params.udpMux()) {
 	if (params.udpMux()) {
-		cout << "ICE UDP mux enabled" << endl;
+		std::cout << "ICE UDP mux enabled" << std::endl;
 		config.enableIceUdpMux = true;
 		config.enableIceUdpMux = true;
 	}
 	}
 
 
 	localId = randomId(4);
 	localId = randomId(4);
-	cout << "The local ID is: " << localId << endl;
+	std::cout << "The local ID is " << localId << std::endl;
 
 
-	auto ws = make_shared<WebSocket>();
+	auto ws = std::make_shared<rtc::WebSocket>();
 
 
 	std::promise<void> wsPromise;
 	std::promise<void> wsPromise;
 	auto wsFuture = wsPromise.get_future();
 	auto wsFuture = wsPromise.get_future();
 
 
 	ws->onOpen([&wsPromise]() {
 	ws->onOpen([&wsPromise]() {
-		cout << "WebSocket connected, signaling ready" << endl;
+		std::cout << "WebSocket connected, signaling ready" << std::endl;
 		wsPromise.set_value();
 		wsPromise.set_value();
 	});
 	});
 
 
-	ws->onError([&wsPromise](string s) {
-		cout << "WebSocket error" << endl;
+	ws->onError([&wsPromise](std::string s) {
+		std::cout << "WebSocket error" << std::endl;
 		wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s)));
 		wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s)));
 	});
 	});
 
 
-	ws->onClosed([]() { cout << "WebSocket closed" << endl; });
+	ws->onClosed([]() { std::cout << "WebSocket closed" << std::endl; });
 
 
-	ws->onMessage([&](variant<binary, string> data) {
-		if (!holds_alternative<string>(data))
+	ws->onMessage([&config, wws = make_weak_ptr(ws)](auto data) {
+		// data holds either std::string or rtc::binary
+		if (!std::holds_alternative<std::string>(data))
 			return;
 			return;
 
 
-		json message = json::parse(get<string>(data));
+		json message = json::parse(std::get<std::string>(data));
 
 
 		auto it = message.find("id");
 		auto it = message.find("id");
 		if (it == message.end())
 		if (it == message.end())
 			return;
 			return;
-		string id = it->get<string>();
+
+		auto id = it->get<std::string>();
 
 
 		it = message.find("type");
 		it = message.find("type");
 		if (it == message.end())
 		if (it == message.end())
 			return;
 			return;
-		string type = it->get<string>();
 
 
-		shared_ptr<PeerConnection> pc;
+		auto type = it->get<std::string>();
+
+		shared_ptr<rtc::PeerConnection> pc;
 		if (auto jt = peerConnectionMap.find(id); jt != peerConnectionMap.end()) {
 		if (auto jt = peerConnectionMap.find(id); jt != peerConnectionMap.end()) {
 			pc = jt->second;
 			pc = jt->second;
 		} else if (type == "offer") {
 		} else if (type == "offer") {
-			cout << "Answering to " + id << endl;
-			pc = createPeerConnection(config, ws, id);
+			std::cout << "Answering to " + id << std::endl;
+			pc = createPeerConnection(config, wws, id);
 		} else {
 		} else {
 			return;
 			return;
 		}
 		}
 
 
 		if (type == "offer" || type == "answer") {
 		if (type == "offer" || type == "answer") {
-			auto sdp = message["description"].get<string>();
-			pc->setRemoteDescription(Description(sdp, type));
+			auto sdp = message["description"].get<std::string>();
+			pc->setRemoteDescription(rtc::Description(sdp, type));
 		} else if (type == "candidate") {
 		} else if (type == "candidate") {
-			auto sdp = message["candidate"].get<string>();
-			auto mid = message["mid"].get<string>();
-			pc->addRemoteCandidate(Candidate(sdp, mid));
+			auto sdp = message["candidate"].get<std::string>();
+			auto mid = message["mid"].get<std::string>();
+			pc->addRemoteCandidate(rtc::Candidate(sdp, mid));
 		}
 		}
 	});
 	});
 
 
-	string wsPrefix = "";
-	if (params.webSocketServer().substr(0, 5).compare("ws://") != 0) {
-		wsPrefix = "ws://";
-	}
-	const string url = wsPrefix + params.webSocketServer() + ":" +
-	                   to_string(params.webSocketPort()) + "/" + localId;
-	cout << "Url is " << url << endl;
+	const std::string wsPrefix =
+	    params.webSocketServer().find("://") == std::string::npos ? "ws://" : "";
+	const std::string url = wsPrefix + params.webSocketServer() + ":" +
+	                        std::to_string(params.webSocketPort()) + "/" + localId;
+
+	std::cout << "WebSocket URL is " << url << std::endl;
 	ws->open(url);
 	ws->open(url);
 
 
-	cout << "Waiting for signaling to be connected..." << endl;
+	std::cout << "Waiting for signaling to be connected..." << std::endl;
 	wsFuture.get();
 	wsFuture.get();
 
 
 	while (true) {
 	while (true) {
-		string id;
-		cout << "Enter a remote ID to send an offer:" << endl;
-		cin >> id;
-		cin.ignore();
+		std::string id;
+		std::cout << "Enter a remote ID to send an offer:" << std::endl;
+		std::cin >> id;
+		std::cin.ignore();
+
 		if (id.empty())
 		if (id.empty())
 			break;
 			break;
-		if (id == localId)
+
+		if (id == localId) {
+			std::cout << "Invalid remote ID (This is the local ID)" << std::endl;
 			continue;
 			continue;
+		}
 
 
-		cout << "Offering to " + id << endl;
+		std::cout << "Offering to " + id << std::endl;
 		auto pc = createPeerConnection(config, ws, id);
 		auto pc = createPeerConnection(config, ws, id);
 
 
 		// We are the offerer, so create a data channel to initiate the process
 		// We are the offerer, so create a data channel to initiate the process
-		const string label = "test";
-		cout << "Creating DataChannel with label \"" << label << "\"" << endl;
+		const std::string label = "test";
+		std::cout << "Creating DataChannel with label \"" << label << "\"" << std::endl;
 		auto dc = pc->createDataChannel(label);
 		auto dc = pc->createDataChannel(label);
 
 
 		dc->onOpen([id, wdc = make_weak_ptr(dc)]() {
 		dc->onOpen([id, wdc = make_weak_ptr(dc)]() {
-			cout << "DataChannel from " << id << " open" << endl;
+			std::cout << "DataChannel from " << id << " open" << std::endl;
 			if (auto dc = wdc.lock())
 			if (auto dc = wdc.lock())
 				dc->send("Hello from " + localId);
 				dc->send("Hello from " + localId);
 		});
 		});
 
 
-		dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; });
+		dc->onClosed([id]() { std::cout << "DataChannel from " << id << " closed" << std::endl; });
 
 
-		dc->onMessage([id, wdc = make_weak_ptr(dc)](variant<binary, string> data) {
-			if (holds_alternative<string>(data))
-				cout << "Message from " << id << " received: " << get<string>(data) << endl;
+		dc->onMessage([id, wdc = make_weak_ptr(dc)](auto data) {
+			// data holds either std::string or rtc::binary
+			if (std::holds_alternative<std::string>(data))
+				std::cout << "Message from " << id << " received: " << std::get<std::string>(data)
+				          << std::endl;
 			else
 			else
-				cout << "Binary message from " << id
-				     << " received, size=" << get<binary>(data).size() << endl;
+				std::cout << "Binary message from " << id
+				          << " received, size=" << std::get<rtc::binary>(data).size() << std::endl;
 		});
 		});
 
 
 		dataChannelMap.emplace(id, dc);
 		dataChannelMap.emplace(id, dc);
 	}
 	}
 
 
-	cout << "Cleaning up..." << endl;
+	std::cout << "Cleaning up..." << std::endl;
 
 
 	dataChannelMap.clear();
 	dataChannelMap.clear();
 	peerConnectionMap.clear();
 	peerConnectionMap.clear();
@@ -196,50 +203,55 @@ int main(int argc, char **argv) try {
 }
 }
 
 
 // Create and setup a PeerConnection
 // Create and setup a PeerConnection
-shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
-                                                weak_ptr<WebSocket> wws, string id) {
-	auto pc = make_shared<PeerConnection>(config);
+shared_ptr<rtc::PeerConnection> createPeerConnection(const rtc::Configuration &config,
+                                                     weak_ptr<rtc::WebSocket> wws, std::string id) {
+	auto pc = std::make_shared<rtc::PeerConnection>(config);
 
 
-	pc->onStateChange([](PeerConnection::State state) { cout << "State: " << state << endl; });
+	pc->onStateChange(
+	    [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
 
 
-	pc->onGatheringStateChange(
-	    [](PeerConnection::GatheringState state) { cout << "Gathering State: " << state << endl; });
+	pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) {
+		std::cout << "Gathering State: " << state << std::endl;
+	});
 
 
-	pc->onLocalDescription([wws, id](Description description) {
-		json message = {
-		    {"id", id}, {"type", description.typeString()}, {"description", string(description)}};
+	pc->onLocalDescription([wws, id](rtc::Description description) {
+		json message = {{"id", id},
+		                {"type", description.typeString()},
+		                {"description", std::string(description)}};
 
 
 		if (auto ws = wws.lock())
 		if (auto ws = wws.lock())
 			ws->send(message.dump());
 			ws->send(message.dump());
 	});
 	});
 
 
-	pc->onLocalCandidate([wws, id](Candidate candidate) {
+	pc->onLocalCandidate([wws, id](rtc::Candidate candidate) {
 		json message = {{"id", id},
 		json message = {{"id", id},
 		                {"type", "candidate"},
 		                {"type", "candidate"},
-		                {"candidate", string(candidate)},
+		                {"candidate", std::string(candidate)},
 		                {"mid", candidate.mid()}};
 		                {"mid", candidate.mid()}};
 
 
 		if (auto ws = wws.lock())
 		if (auto ws = wws.lock())
 			ws->send(message.dump());
 			ws->send(message.dump());
 	});
 	});
 
 
-	pc->onDataChannel([id](shared_ptr<DataChannel> dc) {
-		cout << "DataChannel from " << id << " received with label \"" << dc->label() << "\""
-		     << endl;
+	pc->onDataChannel([id](shared_ptr<rtc::DataChannel> dc) {
+		std::cout << "DataChannel from " << id << " received with label \"" << dc->label() << "\""
+		          << std::endl;
 
 
 		dc->onOpen([wdc = make_weak_ptr(dc)]() {
 		dc->onOpen([wdc = make_weak_ptr(dc)]() {
 			if (auto dc = wdc.lock())
 			if (auto dc = wdc.lock())
 				dc->send("Hello from " + localId);
 				dc->send("Hello from " + localId);
 		});
 		});
 
 
-		dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; });
+		dc->onClosed([id]() { std::cout << "DataChannel from " << id << " closed" << std::endl; });
 
 
-		dc->onMessage([id](variant<binary, string> data) {
-			if (holds_alternative<string>(data))
-				cout << "Message from " << id << " received: " << get<string>(data) << endl;
+		dc->onMessage([id](auto data) {
+			// data holds either std::string or rtc::binary
+			if (std::holds_alternative<std::string>(data))
+				std::cout << "Message from " << id << " received: " << std::get<std::string>(data)
+				          << std::endl;
 			else
 			else
-				cout << "Binary message from " << id
-				     << " received, size=" << get<binary>(data).size() << endl;
+				std::cout << "Binary message from " << id
+				          << " received, size=" << std::get<rtc::binary>(data).size() << std::endl;
 		});
 		});
 
 
 		dataChannelMap.emplace(id, dc);
 		dataChannelMap.emplace(id, dc);
@@ -250,12 +262,12 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 };
 };
 
 
 // Helper function to generate a random ID
 // Helper function to generate a random ID
-string randomId(size_t length) {
-	static const string characters(
+std::string randomId(size_t length) {
+	static const std::string characters(
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
 	    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
-	string id(length, '0');
-	default_random_engine rng(random_device{}());
-	uniform_int_distribution<int> dist(0, int(characters.size() - 1));
-	generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); });
+	std::string id(length, '0');
+	std::default_random_engine rng(std::random_device{}());
+	std::uniform_int_distribution<int> dist(0, int(characters.size() - 1));
+	std::generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); });
 	return id;
 	return id;
 }
 }

+ 64 - 58
examples/copy-paste/answerer.cpp

@@ -23,66 +23,70 @@
 #include <memory>
 #include <memory>
 #include <thread>
 #include <thread>
 
 
-using namespace rtc;
-using namespace std;
-
+using namespace std::chrono_literals;
+using std::shared_ptr;
+using std::weak_ptr;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
-	rtc::InitLogger(LogLevel::Warning);
+	rtc::InitLogger(rtc::LogLevel::Warning);
 
 
-	Configuration config;
+	rtc::Configuration config;
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 
 
-	auto pc = std::make_shared<PeerConnection>(config);
+	auto pc = std::make_shared<rtc::PeerConnection>(config);
 
 
-	pc->onLocalDescription([](Description description) {
-		cout << "Local Description (Paste this to the other peer):" << endl;
-		cout << string(description) << endl;
+	pc->onLocalDescription([](rtc::Description description) {
+		std::cout << "Local Description (Paste this to the other peer):" << std::endl;
+		std::cout << std::string(description) << std::endl;
 	});
 	});
 
 
-	pc->onLocalCandidate([](Candidate candidate) {
-		cout << "Local Candidate (Paste this to the other peer after the local description):"
-		     << endl;
-		cout << string(candidate) << endl << endl;
+	pc->onLocalCandidate([](rtc::Candidate candidate) {
+		std::cout << "Local Candidate (Paste this to the other peer after the local description):"
+		          << std::endl;
+		std::cout << std::string(candidate) << std::endl << std::endl;
 	});
 	});
 
 
-	pc->onStateChange(
-	    [](PeerConnection::State state) { cout << "[State: " << state << "]" << endl; });
-	pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
-		cout << "[Gathering State: " << state << "]" << endl;
+	pc->onStateChange([](rtc::PeerConnection::State state) {
+		std::cout << "[State: " << state << "]" << std::endl;
+	});
+	pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) {
+		std::cout << "[Gathering State: " << state << "]" << std::endl;
 	});
 	});
 
 
-	shared_ptr<DataChannel> dc = nullptr;
-	pc->onDataChannel([&](shared_ptr<DataChannel> _dc) {
-		cout << "[Got a DataChannel with label: " << _dc->label() << "]" << endl;
+	shared_ptr<rtc::DataChannel> dc;
+	pc->onDataChannel([&](shared_ptr<rtc::DataChannel> _dc) {
+		std::cout << "[Got a DataChannel with label: " << _dc->label() << "]" << std::endl;
 		dc = _dc;
 		dc = _dc;
 
 
-		dc->onClosed([&]() { cout << "[DataChannel closed: " << dc->label() << "]" << endl; });
+		dc->onClosed(
+		    [&]() { std::cout << "[DataChannel closed: " << dc->label() << "]" << std::endl; });
 
 
-		dc->onMessage([](variant<binary, string> message) {
-			if (holds_alternative<string>(message)) {
-				cout << "[Received message: " << get<string>(message) << "]" << endl;
+		dc->onMessage([](auto data) {
+			if (std::holds_alternative<std::string>(data)) {
+				std::cout << "[Received message: " << std::get<std::string>(data) << "]"
+				          << std::endl;
 			}
 			}
 		});
 		});
 	});
 	});
 
 
 	bool exit = false;
 	bool exit = false;
 	while (!exit) {
 	while (!exit) {
-		cout << endl
-		     << "**********************************************************************************"
-		        "*****"
-		     << endl
-		     << "* 0: Exit /"
-		     << " 1: Enter remote description /"
-		     << " 2: Enter remote candidate /"
-		     << " 3: Send message /"
-		     << " 4: Print Connection Info *" << endl
-		     << "[Command]: ";
+		std::cout
+		    << std::endl
+		    << "**********************************************************************************"
+		       "*****"
+		    << std::endl
+		    << "* 0: Exit /"
+		    << " 1: Enter remote description /"
+		    << " 2: Enter remote candidate /"
+		    << " 3: Send message /"
+		    << " 4: Print Connection Info *" << std::endl
+		    << "[Command]: ";
 
 
 		int command = -1;
 		int command = -1;
-		cin >> command;
-		cin.ignore();
+		std::cin >> command;
+		std::cin.ignore();
 
 
 		switch (command) {
 		switch (command) {
 		case 0: {
 		case 0: {
@@ -91,9 +95,9 @@ int main(int argc, char **argv) {
 		}
 		}
 		case 1: {
 		case 1: {
 			// Parse Description
 			// Parse Description
-			cout << "[Description]: ";
-			string sdp, line;
-			while (getline(cin, line) && !line.empty()) {
+			std::cout << "[Description]: ";
+			std::string sdp, line;
+			while (getline(std::cin, line) && !line.empty()) {
 				sdp += line;
 				sdp += line;
 				sdp += "\r\n";
 				sdp += "\r\n";
 			}
 			}
@@ -103,48 +107,49 @@ int main(int argc, char **argv) {
 		}
 		}
 		case 2: {
 		case 2: {
 			// Parse Candidate
 			// Parse Candidate
-			cout << "[Candidate]: ";
-			string candidate;
-			getline(cin, candidate);
+			std::cout << "[Candidate]: ";
+			std::string candidate;
+			getline(std::cin, candidate);
 			pc->addRemoteCandidate(candidate);
 			pc->addRemoteCandidate(candidate);
 			break;
 			break;
 		}
 		}
 		case 3: {
 		case 3: {
 			// Send Message
 			// Send Message
 			if (!dc || !dc->isOpen()) {
 			if (!dc || !dc->isOpen()) {
-				cout << "** Channel is not Open ** ";
+				std::cout << "** Channel is not Open ** ";
 				break;
 				break;
 			}
 			}
-			cout << "[Message]: ";
-			string message;
-			getline(cin, message);
+			std::cout << "[Message]: ";
+			std::string message;
+			getline(std::cin, message);
 			dc->send(message);
 			dc->send(message);
 			break;
 			break;
 		}
 		}
 		case 4: {
 		case 4: {
 			// Connection Info
 			// Connection Info
 			if (!dc || !dc->isOpen()) {
 			if (!dc || !dc->isOpen()) {
-				cout << "** Channel is not Open ** ";
+				std::cout << "** Channel is not Open ** ";
 				break;
 				break;
 			}
 			}
-			Candidate local, remote;
+			rtc::Candidate local, remote;
 			std::optional<std::chrono::milliseconds> rtt = pc->rtt();
 			std::optional<std::chrono::milliseconds> rtt = pc->rtt();
 			if (pc->getSelectedCandidatePair(&local, &remote)) {
 			if (pc->getSelectedCandidatePair(&local, &remote)) {
-				cout << "Local: " << local << endl;
-				cout << "Remote: " << remote << endl;
-				cout << "Bytes Sent:" << pc->bytesSent()
-				     << " / Bytes Received:" << pc->bytesReceived() << " / Round-Trip Time:";
+				std::cout << "Local: " << local << std::endl;
+				std::cout << "Remote: " << remote << std::endl;
+				std::cout << "Bytes Sent:" << pc->bytesSent()
+				          << " / Bytes Received:" << pc->bytesReceived() << " / Round-Trip Time:";
 				if (rtt.has_value())
 				if (rtt.has_value())
-					cout << rtt.value().count();
+					std::cout << rtt.value().count();
 				else
 				else
-					cout << "null";
-				cout << " ms";
-			} else
-				cout << "Could not get Candidate Pair Info" << endl;
+					std::cout << "null";
+				std::cout << " ms";
+			} else {
+				std::cout << "Could not get Candidate Pair Info" << std::endl;
+			}
 			break;
 			break;
 		}
 		}
 		default: {
 		default: {
-			cout << "** Invalid Command ** ";
+			std::cout << "** Invalid Command ** " << std::endl;
 			break;
 			break;
 		}
 		}
 		}
 		}
@@ -152,6 +157,7 @@ int main(int argc, char **argv) {
 
 
 	if (dc)
 	if (dc)
 		dc->close();
 		dc->close();
+
 	if (pc)
 	if (pc)
 		pc->close();
 		pc->close();
 }
 }

+ 62 - 57
examples/copy-paste/offerer.cpp

@@ -23,67 +23,70 @@
 #include <memory>
 #include <memory>
 #include <thread>
 #include <thread>
 
 
-using namespace rtc;
-using namespace std;
-
+using namespace std::chrono_literals;
+using std::shared_ptr;
+using std::weak_ptr;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
-	rtc::InitLogger(LogLevel::Warning);
+	rtc::InitLogger(rtc::LogLevel::Warning);
 
 
-	Configuration config;
+	rtc::Configuration config;
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 
 
-	auto pc = std::make_shared<PeerConnection>(config);
+	auto pc = std::make_shared<rtc::PeerConnection>(config);
 
 
-	pc->onLocalDescription([](Description description) {
-		cout << "Local Description (Paste this to the other peer):" << endl;
-		cout << string(description) << endl;
+	pc->onLocalDescription([](rtc::Description description) {
+		std::cout << "Local Description (Paste this to the other peer):" << std::endl;
+		std::cout << std::string(description) << std::endl;
 	});
 	});
 
 
-	pc->onLocalCandidate([](Candidate candidate) {
-		cout << "Local Candidate (Paste this to the other peer after the local description):"
-		     << endl;
-		cout << string(candidate) << endl << endl;
+	pc->onLocalCandidate([](rtc::Candidate candidate) {
+		std::cout << "Local Candidate (Paste this to the other peer after the local description):"
+		          << std::endl;
+		std::cout << std::string(candidate) << std::endl << std::endl;
 	});
 	});
 
 
-	pc->onStateChange(
-	    [](PeerConnection::State state) { cout << "[State: " << state << "]" << endl; });
+	pc->onStateChange([](rtc::PeerConnection::State state) {
+		std::cout << "[State: " << state << "]" << std::endl;
+	});
 
 
-	pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
-		cout << "[Gathering State: " << state << "]" << endl;
+	pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) {
+		std::cout << "[Gathering State: " << state << "]" << std::endl;
 	});
 	});
 
 
 	auto dc = pc->createDataChannel("test"); // this is the offerer, so create a data channel
 	auto dc = pc->createDataChannel("test"); // this is the offerer, so create a data channel
 
 
-	dc->onOpen([&]() { cout << "[DataChannel open: " << dc->label() << "]" << endl; });
+	dc->onOpen([&]() { std::cout << "[DataChannel open: " << dc->label() << "]" << std::endl; });
 
 
-	dc->onClosed([&]() { cout << "[DataChannel closed: " << dc->label() << "]" << endl; });
+	dc->onClosed(
+	    [&]() { std::cout << "[DataChannel closed: " << dc->label() << "]" << std::endl; });
 
 
-	dc->onMessage([](variant<binary, string> message) {
-		if (holds_alternative<string>(message)) {
-			cout << "[Received: " << get<string>(message) << "]" << endl;
+	dc->onMessage([](auto data) {
+		if (std::holds_alternative<std::string>(data)) {
+			std::cout << "[Received: " << std::get<std::string>(data) << "]" << std::endl;
 		}
 		}
 	});
 	});
 
 
-	this_thread::sleep_for(1s);
+	std::this_thread::sleep_for(1s);
 
 
 	bool exit = false;
 	bool exit = false;
 	while (!exit) {
 	while (!exit) {
-		cout << endl
-		     << "**********************************************************************************"
-		        "*****"
-		     << endl
-		     << "* 0: Exit /"
-		     << " 1: Enter remote description /"
-		     << " 2: Enter remote candidate /"
-		     << " 3: Send message /"
-		     << " 4: Print Connection Info *" << endl
-		     << "[Command]: ";
+		std::cout
+		    << std::endl
+		    << "**********************************************************************************"
+		       "*****"
+		    << std::endl
+		    << "* 0: Exit /"
+		    << " 1: Enter remote description /"
+		    << " 2: Enter remote candidate /"
+		    << " 3: Send message /"
+		    << " 4: Print Connection Info *" << std::endl
+		    << "[Command]: ";
 
 
 		int command = -1;
 		int command = -1;
-		cin >> command;
-		cin.ignore();
+		std::cin >> command;
+		std::cin.ignore();
 
 
 		switch (command) {
 		switch (command) {
 		case 0: {
 		case 0: {
@@ -92,9 +95,9 @@ int main(int argc, char **argv) {
 		}
 		}
 		case 1: {
 		case 1: {
 			// Parse Description
 			// Parse Description
-			cout << "[Description]: ";
-			string sdp, line;
-			while (getline(cin, line) && !line.empty()) {
+			std::cout << "[Description]: ";
+			std::string sdp, line;
+			while (getline(std::cin, line) && !line.empty()) {
 				sdp += line;
 				sdp += line;
 				sdp += "\r\n";
 				sdp += "\r\n";
 			}
 			}
@@ -103,48 +106,49 @@ int main(int argc, char **argv) {
 		}
 		}
 		case 2: {
 		case 2: {
 			// Parse Candidate
 			// Parse Candidate
-			cout << "[Candidate]: ";
-			string candidate;
-			getline(cin, candidate);
+			std::cout << "[Candidate]: ";
+			std::string candidate;
+			getline(std::cin, candidate);
 			pc->addRemoteCandidate(candidate);
 			pc->addRemoteCandidate(candidate);
 			break;
 			break;
 		}
 		}
 		case 3: {
 		case 3: {
 			// Send Message
 			// Send Message
 			if (!dc->isOpen()) {
 			if (!dc->isOpen()) {
-				cout << "** Channel is not Open ** ";
+				std::cout << "** Channel is not Open ** ";
 				break;
 				break;
 			}
 			}
-			cout << "[Message]: ";
-			string message;
-			getline(cin, message);
+			std::cout << "[Message]: ";
+			std::string message;
+			getline(std::cin, message);
 			dc->send(message);
 			dc->send(message);
 			break;
 			break;
 		}
 		}
 		case 4: {
 		case 4: {
 			// Connection Info
 			// Connection Info
 			if (!dc || !dc->isOpen()) {
 			if (!dc || !dc->isOpen()) {
-				cout << "** Channel is not Open ** ";
+				std::cout << "** Channel is not Open ** ";
 				break;
 				break;
 			}
 			}
-			Candidate local, remote;
+			rtc::Candidate local, remote;
 			std::optional<std::chrono::milliseconds> rtt = pc->rtt();
 			std::optional<std::chrono::milliseconds> rtt = pc->rtt();
 			if (pc->getSelectedCandidatePair(&local, &remote)) {
 			if (pc->getSelectedCandidatePair(&local, &remote)) {
-				cout << "Local: " << local << endl;
-				cout << "Remote: " << remote << endl;
-				cout << "Bytes Sent:" << pc->bytesSent()
-				     << " / Bytes Received:" << pc->bytesReceived() << " / Round-Trip Time:";
+				std::cout << "Local: " << local << std::endl;
+				std::cout << "Remote: " << remote << std::endl;
+				std::cout << "Bytes Sent:" << pc->bytesSent()
+				          << " / Bytes Received:" << pc->bytesReceived() << " / Round-Trip Time:";
 				if (rtt.has_value())
 				if (rtt.has_value())
-					cout << rtt.value().count();
+					std::cout << rtt.value().count();
 				else
 				else
-					cout << "null";
-				cout << " ms";
-			} else
-				cout << "Could not get Candidate Pair Info" << endl;
+					std::cout << "null";
+				std::cout << " ms";
+			} else {
+				std::cout << "Could not get Candidate Pair Info" << std::endl;
+			}
 			break;
 			break;
 		}
 		}
 		default: {
 		default: {
-			cout << "** Invalid Command ** ";
+			std::cout << "** Invalid Command **" << std::endl;
 			break;
 			break;
 		}
 		}
 		}
 		}
@@ -152,6 +156,7 @@ int main(int argc, char **argv) {
 
 
 	if (dc)
 	if (dc)
 		dc->close();
 		dc->close();
+
 	if (pc)
 	if (pc)
 		pc->close();
 		pc->close();
 }
 }

+ 4 - 5
examples/media/main.cpp

@@ -17,8 +17,6 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
  */
 
 
-#define _WINSOCK_DEPRECATED_NO_WARNINGS
-
 #include "rtc/rtc.hpp"
 #include "rtc/rtc.hpp"
 
 
 #include <iostream>
 #include <iostream>
@@ -28,11 +26,12 @@
 #include <nlohmann/json.hpp>
 #include <nlohmann/json.hpp>
 
 
 #ifdef _WIN32
 #ifdef _WIN32
+#define _WINSOCK_DEPRECATED_NO_WARNINGS
 #include <winsock2.h>
 #include <winsock2.h>
 #else
 #else
-#include <sys/socket.h>
 #include <arpa/inet.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
+#include <sys/socket.h>
 typedef int SOCKET;
 typedef int SOCKET;
 #endif
 #endif
 
 
@@ -57,10 +56,10 @@ int main() {
 		});
 		});
 
 
 		SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
 		SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
-		sockaddr_in addr;
+		sockaddr_in addr = {};
+		addr.sin_family = AF_INET;
 		addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 		addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 		addr.sin_port = htons(5000);
 		addr.sin_port = htons(5000);
-		addr.sin_family = AF_INET;
 
 
 		rtc::Description::Video media("video", rtc::Description::Direction::RecvOnly);
 		rtc::Description::Video media("video", rtc::Description::Direction::RecvOnly);
 		media.addH264Codec(96);
 		media.addH264Codec(96);

+ 3 - 3
examples/sfu-media/main.cpp

@@ -17,12 +17,11 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
  */
 
 
-#define _WINSOCK_DEPRECATED_NO_WARNINGS
-
 #include "rtc/rtc.hpp"
 #include "rtc/rtc.hpp"
 
 
 #include <iostream>
 #include <iostream>
 #include <memory>
 #include <memory>
+#include <vector>
 
 
 #include <nlohmann/json.hpp>
 #include <nlohmann/json.hpp>
 
 
@@ -32,6 +31,7 @@ struct Receiver {
 	std::shared_ptr<rtc::PeerConnection> conn;
 	std::shared_ptr<rtc::PeerConnection> conn;
 	std::shared_ptr<rtc::Track> track;
 	std::shared_ptr<rtc::Track> track;
 };
 };
+
 int main() {
 int main() {
 	std::vector<std::shared_ptr<Receiver>> receivers;
 	std::vector<std::shared_ptr<Receiver>> receivers;
 
 
@@ -77,7 +77,7 @@ int main() {
 		    },
 		    },
 		    nullptr);
 		    nullptr);
 
 
-		// Set the SENDERS Answer
+		// Set the sender's answer
 		{
 		{
 			std::cout << "Please copy/paste the answer provided by the SENDER: " << std::endl;
 			std::cout << "Please copy/paste the answer provided by the SENDER: " << std::endl;
 			std::string sdp;
 			std::string sdp;