Browse Source

Added local and remote address getters to C API

Paul-Louis Ageneau 5 years ago
parent
commit
ed28460e80
3 changed files with 42 additions and 1 deletions
  1. 3 0
      include/rtc/rtc.h
  2. 28 0
      src/rtc.cpp
  3. 11 1
      test/capi.cpp

+ 3 - 0
include/rtc/rtc.h

@@ -88,6 +88,9 @@ int rtcSetGatheringStateChangeCallback(int pc, gatheringStateCallbackFunc cb);
 int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
 int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
 
+int rtcGetLocalAddress(int pc, char *buffer, int size);
+int rtcGetRemoteAddress(int pc, char *buffer, int size);
+
 // DataChannel
 int rtcCreateDataChannel(int pc, const char *label);
 int rtcDeleteDataChannel(int dc);

+ 28 - 0
src/rtc.cpp

@@ -247,6 +247,34 @@ int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid) {
 	return 0;
 }
 
+int rtcGetLocalAddress(int pc, char *buffer, int size) {
+	auto peerConnection = getPeerConnection(pc);
+	if (!peerConnection)
+		return -1;
+
+	if (auto addr = peerConnection->localAddress()) {
+		size = std::min(size_t(size - 1), addr->size());
+		std::copy(addr->data(), addr->data() + size, buffer);
+		buffer[size] = '\0';
+		return size + 1;
+	}
+	return -1;
+}
+
+int rtcGetRemoteAddress(int pc, char *buffer, int size) {
+	auto peerConnection = getPeerConnection(pc);
+	if (!peerConnection)
+		return -1;
+
+	if (auto addr = peerConnection->remoteAddress()) {
+		size = std::min(size_t(size - 1), addr->size());
+		std::copy(addr->data(), addr->data() + size, buffer);
+		buffer[size] = '\0';
+		return size + 1;
+	}
+	return -1;
+}
+
 int rtcGetDataChannelLabel(int dc, char *buffer, int size) {
 	auto dataChannel = getDataChannel(dc);
 	if (!dataChannel)

+ 11 - 1
test/capi.cpp

@@ -156,6 +156,16 @@ int test_capi_main() {
 
 	sleep(3);
 
+	char buffer[256];
+	if (rtcGetLocalAddress(peer1->pc, buffer, 256) >= 0)
+		printf("Local address 1:  %s\n", buffer);
+	if (rtcGetRemoteAddress(peer1->pc, buffer, 256) >= 0)
+		printf("Remote address 1: %s\n", buffer);
+	if (rtcGetLocalAddress(peer2->pc, buffer, 256) >= 0)
+		printf("Local address 2:  %s\n", buffer);
+	if (rtcGetRemoteAddress(peer2->pc, buffer, 256) >= 0)
+		printf("Remote address 2: %s\n", buffer);
+
 	if (peer1->connected && peer2->connected) {
 		deletePeer(peer1);
 		deletePeer(peer2);
@@ -174,5 +184,5 @@ error:
 
 void test_capi() {
 	if (test_capi_main())
-		throw std::runtime_error("C API test failed");
+		throw std::runtime_error("Connection failed");
 }