Browse Source

Added rtcDelete accepting DataChannel, Track, or WebSocket to C API

Paul-Louis Ageneau 2 years ago
parent
commit
7dee6fed5c
3 changed files with 36 additions and 0 deletions
  1. 1 0
      include/rtc/rtc.h
  2. 33 0
      src/capi.cpp
  3. 2 0
      test/capi_connectivity.cpp

+ 1 - 0
include/rtc/rtc.h

@@ -201,6 +201,7 @@ RTC_EXPORT int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
 RTC_EXPORT int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb);
 RTC_EXPORT int rtcSendMessage(int id, const char *data, int size);
 RTC_EXPORT int rtcClose(int id);
+RTC_EXPORT int rtcDelete(int id);
 RTC_EXPORT bool rtcIsOpen(int id);
 RTC_EXPORT bool rtcIsClosed(int id);
 

+ 33 - 0
src/capi.cpp

@@ -170,6 +170,30 @@ shared_ptr<Channel> getChannel(int id) {
 	throw std::invalid_argument("DataChannel, Track, or WebSocket ID does not exist");
 }
 
+void eraseChannel(int id) {
+	std::lock_guard lock(mutex);
+	if (dataChannelMap.erase(id) != 0) {
+		userPointerMap.erase(id);
+		return;
+	}
+	if (trackMap.erase(id) != 0) {
+		userPointerMap.erase(id);
+#if RTC_ENABLE_MEDIA
+		rtcpSrReporterMap.erase(id);
+		rtcpChainableHandlerMap.erase(id);
+		rtpConfigMap.erase(id);
+#endif
+		return;
+	}
+#if RTC_ENABLE_WEBSOCKET
+	if (webSocketMap.erase(id) != 0) {
+		userPointerMap.erase(id);
+		return;
+	}
+#endif
+	throw std::invalid_argument("DataChannel, Track, or WebSocket ID does not exist");
+}
+
 int copyAndReturn(string s, char *buffer, int size) {
 	if (!buffer)
 		return int(s.size() + 1);
@@ -716,6 +740,15 @@ int rtcClose(int id) {
 	});
 }
 
+int rtcDelete(int id) {
+	return wrap([id] {
+		auto channel = getChannel(id);
+		channel->close();
+		eraseChannel(id);
+		return RTC_ERR_SUCCESS;
+	});
+}
+
 bool rtcIsOpen(int id) {
 	return wrap([id] { return getChannel(id)->isOpen() ? 0 : 1; }) == 0 ? true : false;
 }

+ 2 - 0
test/capi_connectivity.cpp

@@ -350,6 +350,8 @@ int test_capi_connectivity_main() {
 		goto error;
 	}
 
+	rtcClose(peer1->dc); // optional
+
 	rtcClosePeerConnection(peer1->pc); // optional
 
 	deletePeer(peer1);