From e1f60cd34db5c279527a9e07b5247c1e77cefe04 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Sat, 17 Oct 2020 23:29:31 +0200 Subject: [PATCH] Added rtcGetSelectedCandidatePair() to C API --- include/rtc/rtc.h | 2 ++ src/capi.cpp | 31 +++++++++++++++++++++++++++++++ test/capi_connectivity.cpp | 17 +++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/include/rtc/rtc.h b/include/rtc/rtc.h index bcbac29..f1e43f6 100644 --- a/include/rtc/rtc.h +++ b/include/rtc/rtc.h @@ -127,6 +127,8 @@ RTC_EXPORT int rtcGetRemoteDescription(int pc, char *buffer, int size); RTC_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size); RTC_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size); +RTC_EXPORT int rtcGetSelectedCandidatePair(int pc, char *local, int localSize, char *remote, int remoteSize); + // DataChannel RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb); RTC_EXPORT int rtcAddDataChannel(int pc, const char *label); // returns dc id diff --git a/src/capi.cpp b/src/capi.cpp index 35cc466..c2cae6f 100644 --- a/src/capi.cpp +++ b/src/capi.cpp @@ -619,6 +619,37 @@ int rtcGetRemoteAddress(int pc, char *buffer, int size) { }); } +int rtcGetSelectedCandidatePair(int pc, char *local, int localSize, char *remote, int remoteSize) { + return WRAP({ + auto peerConnection = getPeerConnection(pc); + + if (!local) + localSize = 0; + if (!remote) + remoteSize = 0; + + Candidate localCand; + Candidate remoteCand; + if (peerConnection->getSelectedCandidatePair(&localCand, &remoteCand)) { + if (localSize > 0) { + string localSdp = string(localCand); + localSize = std::min(localSize - 1, int(localSdp.size())); + std::copy(localSdp.begin(), localSdp.begin() + localSize, local); + local[localSize] = '\0'; + } + if (remoteSize > 0) { + string remoteSdp = string(remoteCand); + remoteSize = std::min(remoteSize - 1, int(remoteSdp.size())); + std::copy(remoteSdp.begin(), remoteSdp.begin() + remoteSize, remote); + remote[remoteSize] = '\0'; + } + return localSize + remoteSize; + } + + return RTC_ERR_FAILURE; + }); +} + int rtcGetDataChannelLabel(int dc, char *buffer, int size) { return WRAP({ auto dataChannel = getDataChannel(dc); diff --git a/test/capi_connectivity.cpp b/test/capi_connectivity.cpp index 2c80f16..c438ed1 100644 --- a/test/capi_connectivity.cpp +++ b/test/capi_connectivity.cpp @@ -186,6 +186,7 @@ int test_capi_connectivity_main() { } char buffer[BUFFER_SIZE]; + char buffer2[BUFFER_SIZE]; if (rtcGetLocalDescription(peer1->pc, buffer, BUFFER_SIZE) < 0) { fprintf(stderr, "rtcGetLocalDescription failed\n"); @@ -235,6 +236,22 @@ int test_capi_connectivity_main() { } printf("Remote address 2: %s\n", buffer); + + if (rtcGetSelectedCandidatePair(peer1->pc, buffer, BUFFER_SIZE, buffer2, BUFFER_SIZE) < 0) { + fprintf(stderr, "rtcGetSelectedCandidatePair failed\n"); + goto error; + } + printf("Local candidate 1: %s\n", buffer); + printf("Remote candidate 1: %s\n", buffer2); + + if (rtcGetSelectedCandidatePair(peer2->pc, buffer, BUFFER_SIZE, buffer2, BUFFER_SIZE) < 0) { + fprintf(stderr, "rtcGetSelectedCandidatePair failed\n"); + goto error; + } + printf("Local candidate 2: %s\n", buffer); + printf("Remote candidate 2: %s\n", buffer2); + + deletePeer(peer1); sleep(1); deletePeer(peer2);