Răsfoiți Sursa

Get Selected Candidate Pair Info

Murat Dogan 5 ani în urmă
părinte
comite
fc595fd1bb

+ 9 - 0
include/rtc/candidate.hpp

@@ -25,6 +25,15 @@
 
 namespace rtc {
 
+#if not USE_JUICE
+struct CandidateInfo {
+	string address;
+	int port;
+	string type;
+	string transportType;
+};
+#endif
+
 class Candidate {
 public:
 	Candidate(string candidate, string mid = "");

+ 4 - 0
include/rtc/peerconnection.hpp

@@ -87,6 +87,10 @@ public:
 	void onStateChange(std::function<void(State state)> callback);
 	void onGatheringStateChange(std::function<void(GatheringState state)> callback);
 
+#if not USE_JUICE
+	bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote);
+#endif
+
 private:
 	init_token mInitToken = Init::Token();
 

+ 55 - 0
src/icetransport.cpp

@@ -656,6 +656,61 @@ void IceTransport::LogCallback(const gchar *logDomain, GLogLevelFlags logLevel,
 	PLOG(severity) << "nice: " << message;
 }
 
+bool IceTransport::getSelectedCandidatePair(CandidateInfo *localInfo, CandidateInfo *remoteInfo) {
+	NiceCandidate *local, *remote;
+	gboolean result = nice_agent_get_selected_pair(mNiceAgent.get(), mStreamId, 1, &local, &remote);
+
+	if (!result)
+		return false;
+
+	char ipaddr[INET6_ADDRSTRLEN];
+	nice_address_to_string(&local->addr, ipaddr);
+	localInfo->address = std::string(ipaddr);
+	localInfo->port = nice_address_get_port(&local->addr);
+	localInfo->type = IceTransport::CandidateTypeToString(local->type);
+	localInfo->transportType = IceTransport::CandidateTransportTypeToString(local->transport);
+
+	nice_address_to_string(&remote->addr, ipaddr);
+	remoteInfo->address = std::string(ipaddr);
+	remoteInfo->port = nice_address_get_port(&remote->addr);
+	remoteInfo->type = IceTransport::CandidateTypeToString(remote->type);
+	remoteInfo->transportType = IceTransport::CandidateTransportTypeToString(remote->transport);
+
+	nice_candidate_free(local);
+	nice_candidate_free(remote);
+	return true;
+}
+
+const std::string IceTransport::CandidateTypeToString(NiceCandidateType type) {
+	switch (type) {
+	case NiceCandidateType::NICE_CANDIDATE_TYPE_HOST:
+		return "HOST";
+	case NiceCandidateType::NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
+		return "PEER_REFLEXIVE";
+	case NiceCandidateType::NICE_CANDIDATE_TYPE_RELAYED:
+		return "RELAYED";
+	case NiceCandidateType::NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
+		return "SERVER_REFLEXIVE";
+	default:
+		break;
+	}
+}
+
+const std::string IceTransport::CandidateTransportTypeToString(NiceCandidateTransport type) {
+	switch (type) {
+	case NiceCandidateTransport::NICE_CANDIDATE_TRANSPORT_TCP_ACTIVE:
+		return "TCP_ACTIVE";
+	case NiceCandidateTransport::NICE_CANDIDATE_TRANSPORT_TCP_PASSIVE:
+		return "TCP_PASSIVE";
+	case NiceCandidateTransport::NICE_CANDIDATE_TRANSPORT_TCP_SO:
+		return "TCP_SO";
+	case NiceCandidateTransport::NICE_CANDIDATE_TRANSPORT_UDP:
+		return "UDP";
+	default:
+		break;
+	}
+}
+
 } // namespace rtc
 
 #endif

+ 5 - 1
src/icetransport.hpp

@@ -37,7 +37,7 @@
 #include <thread>
 
 namespace rtc {
-
+	
 class IceTransport : public Transport {
 public:
 #if USE_JUICE
@@ -56,6 +56,8 @@ public:
 		Completed = NICE_COMPONENT_STATE_READY,
 		Failed = NICE_COMPONENT_STATE_FAILED,
 	};
+
+	bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote);
 #endif
 	enum class GatheringState { New = 0, InProgress = 1, Complete = 2 };
 
@@ -133,6 +135,8 @@ private:
 	static gboolean TimeoutCallback(gpointer userData);
 	static void LogCallback(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message,
 	                        gpointer user_data);
+	static const std::string CandidateTypeToString(NiceCandidateType type);
+	static const std::string CandidateTransportTypeToString(NiceCandidateTransport type);
 #endif
 };
 

+ 7 - 0
src/peerconnection.cpp

@@ -569,6 +569,13 @@ void PeerConnection::resetCallbacks() {
 	mGatheringStateChangeCallback = nullptr;
 }
 
+#if not USE_JUICE
+bool PeerConnection::getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote) {
+	auto iceTransport = std::atomic_load(&mIceTransport);
+	return iceTransport->getSelectedCandidatePair(local, remote);
+}
+#endif
+
 } // namespace rtc
 
 std::ostream &operator<<(std::ostream &out, const rtc::PeerConnection::State &state) {

+ 18 - 1
test/p2p/answerer.cpp

@@ -76,7 +76,8 @@ int main(int argc, char **argv) {
 		     << "* 0: Exit /"
 		     << " 1: Enter remote description /"
 		     << " 2: Enter remote candidate /"
-		     << " 3: Send message *" << endl
+		     << " 3: Send message /"
+		     << " 4: Print Connection Info *" << endl
 		     << "[Command]: ";
 
 		int command = -1;
@@ -120,6 +121,22 @@ int main(int argc, char **argv) {
 			dc->send(message);
 			break;
 		}
+		case 4: {
+			// Connection Info
+			if (!dc || !dc->isOpen()) {
+				cout << "** Channel is not Open ** ";
+				break;
+			}
+			CandidateInfo local, remote;
+			if (pc->getSelectedCandidatePair(&local, &remote)) {
+				cout << "Local: " << local.address << ":" << local.port << " " << local.type << " "
+				     << local.transportType << endl;
+				cout << "Remote: " << remote.address << ":" << remote.port << " " << remote.type
+				     << " " << remote.transportType << endl;
+			} else
+				cout << "Could not get Candidate Pair Info" << endl;
+			break;
+		}
 		default: {
 			cout << "** Invalid Command ** ";
 			break;

+ 18 - 1
test/p2p/offerer.cpp

@@ -77,7 +77,8 @@ int main(int argc, char **argv) {
 		     << "* 0: Exit /"
 		     << " 1: Enter remote description /"
 		     << " 2: Enter remote candidate /"
-		     << " 3: Send message *" << endl
+		     << " 3: Send message /"
+		     << " 4: Print Connection Info *" << endl
 		     << "[Command]: ";
 
 		int command = -1;
@@ -120,6 +121,22 @@ int main(int argc, char **argv) {
 			dc->send(message);
 			break;
 		}
+		case 4: {
+			// Connection Info
+			if (!dc || !dc->isOpen()) {
+				cout << "** Channel is not Open ** ";
+				break;
+			}
+			CandidateInfo local, remote;
+			if (pc->getSelectedCandidatePair(&local, &remote)) {
+				cout << "Local: " << local.address << ":" << local.port << " " << local.type << " "
+				     << local.transportType << endl;
+				cout << "Remote: " << remote.address << ":" << remote.port << " " << remote.type
+				     << " " << remote.transportType << endl;
+			} else
+				cout << "Could not get Candidate Pair Info" << endl;
+			break;
+		}
 		default: {
 			cout << "** Invalid Command ** ";
 			break;