2
0
Эх сурвалжийг харах

Merge branch 'paullouisageneau:master' into proxy

web2098 2 жил өмнө
parent
commit
5f81176362

+ 12 - 2
BUILDING.md

@@ -1,9 +1,16 @@
 # libdatachannel - Building instructions
 
-## Clone repository and submodules
+## Clone repository
 
 ```bash
 $ git clone https://github.com/paullouisageneau/libdatachannel.git
+```
+
+## Init submodules
+
+This step is optional if `PREFER_SYSTEM_LIB` CMake option will be enabled.
+
+```bash
 $ cd libdatachannel
 $ git submodule update --init --recursive --depth 1
 ```
@@ -12,7 +19,10 @@ $ git submodule update --init --recursive --depth 1
 
 The CMake library targets `libdatachannel` and `libdatachannel-static` respectively correspond to the shared and static libraries. The default target will build tests and examples.
 
-The option `USE_GNUTLS` allows to switch between OpenSSL (default) and GnuTLS, and the option `USE_NICE` allows to switch between libjuice as submodule (default) and libnice. The options `USE_SYSTEM_SRTP` and `USE_SYSTEM_JUICE` allow to link against the system library rather than building the submodule, for libsrtp and libjuice respectively.
+The option `USE_GNUTLS` allows to switch between OpenSSL (default) and GnuTLS, and the option `USE_NICE` allows to switch between libjuice as submodule (default) and libnice.
+
+The option `PREFER_SYSTEM_LIB` allow to link against the system library rather than building all the submodule.
+Options `USE_SYSTEM_SRTP`, `USE_SYSTEM_JUICE`, `USE_SYSTEM_USRSCTP`, `USE_SYSTEM_PLOG` and `USE_SYSTEM_JSON` allow to do the same but per submodule, for libsrtp, libjuice, libusrsctp, Plog and Nlohmann JSON respectively.
 
 If you only need Data Channels, the option `NO_MEDIA` allows to make the library lighter by removing media support. Similarly, `NO_WEBSOCKET` removes WebSocket support.
 

+ 34 - 17
CMakeLists.txt

@@ -7,8 +7,12 @@ set(PROJECT_DESCRIPTION "C/C++ WebRTC network library featuring Data Channels, M
 # Options
 option(USE_GNUTLS "Use GnuTLS instead of OpenSSL" OFF)
 option(USE_NICE "Use libnice instead of libjuice" OFF)
-option(USE_SYSTEM_SRTP "Use system libSRTP" OFF)
-option(USE_SYSTEM_JUICE "Use system libjuice" OFF)
+option(PREFER_SYSTEM_LIB "Prefer system libraries over deps folder" OFF)
+option(USE_SYSTEM_SRTP "Use system libSRTP" ${PREFER_SYSTEM_LIB})
+option(USE_SYSTEM_JUICE "Use system libjuice" ${PREFER_SYSTEM_LIB})
+option(USE_SYSTEM_USRSCTP "Use system libusrsctp" ${PREFER_SYSTEM_LIB})
+option(USE_SYSTEM_PLOG "Use system Plog" ${PREFER_SYSTEM_LIB})
+option(USE_SYSTEM_JSON "Use system Nlohmann JSON" ${PREFER_SYSTEM_LIB})
 option(NO_WEBSOCKET "Disable WebSocket support" OFF)
 option(NO_MEDIA "Disable media transport support" OFF)
 option(NO_EXAMPLES "Disable examples" OFF)
@@ -204,25 +208,34 @@ set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
 set(THREADS_PREFER_PTHREAD_FLAG TRUE)
 find_package(Threads REQUIRED)
 
-set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
-add_subdirectory(deps/plog EXCLUDE_FROM_ALL)
+if(USE_SYSTEM_PLOG)
+	find_package(plog REQUIRED)
+else()
+	set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
+	add_subdirectory(deps/plog EXCLUDE_FROM_ALL)
+endif()
 
 if(SCTP_DEBUG)
 	add_definitions(-DSCTP_DEBUG)
 endif()
-option(sctp_build_shared_lib OFF)
-option(sctp_build_programs OFF)
-option(sctp_inet OFF)
-option(sctp_inet6 OFF)
-set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
-add_subdirectory(deps/usrsctp EXCLUDE_FROM_ALL)
-if (MSYS OR MINGW)
-	target_compile_definitions(usrsctp PUBLIC -DSCTP_STDINT_INCLUDE=<stdint.h>)
-endif()
-if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
-    target_compile_options(usrsctp PRIVATE -Wno-error=format-truncation)
+
+if(USE_SYSTEM_USRSCTP)
+	find_package(Usrsctp REQUIRED)
+else()
+	option(sctp_build_shared_lib OFF)
+	option(sctp_build_programs OFF)
+	option(sctp_inet OFF)
+	option(sctp_inet6 OFF)
+	set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
+	add_subdirectory(deps/usrsctp EXCLUDE_FROM_ALL)
+	if (MSYS OR MINGW)
+		target_compile_definitions(usrsctp PUBLIC -DSCTP_STDINT_INCLUDE=<stdint.h>)
+	endif()
+	if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
+    	target_compile_options(usrsctp PRIVATE -Wno-error=format-truncation)
+	endif()
+	add_library(Usrsctp::Usrsctp ALIAS usrsctp)
 endif()
-add_library(Usrsctp::Usrsctp ALIAS usrsctp)
 
 add_library(datachannel SHARED
 	${LIBDATACHANNEL_SOURCES}
@@ -475,7 +488,11 @@ endif()
 # Examples
 if(NOT NO_EXAMPLES)
 	set(JSON_BuildTests OFF CACHE INTERNAL "")
-	add_subdirectory(deps/json EXCLUDE_FROM_ALL)
+	if(USE_SYSTEM_JSON)
+		find_package(nlohmann_json REQUIRED)
+	else()
+		add_subdirectory(deps/json EXCLUDE_FROM_ALL)
+	endif()
 
 	if(NOT NO_WEBSOCKET)
 		add_subdirectory(examples/client)

+ 6 - 7
README.md

@@ -24,13 +24,12 @@ libdatachannel is available on [AUR](https://aur.archlinux.org/packages/libdatac
 
 ## Dependencies
 
-Only [GnuTLS](https://www.gnutls.org/) or [OpenSSL](https://www.openssl.org/) is necessary. Optionally, [libnice](https://nice.freedesktop.org/) can be selected as an alternative ICE backend instead of libjuice.
-
-Submodules:
-- usrsctp: https://github.com/sctplab/usrsctp
-- plog: https://github.com/SergiusTheBest/plog
-- libjuice: https://github.com/paullouisageneau/libjuice (if not compiled with libnice backend)
-- libsrtp: https://github.com/cisco/libsrtp (if compiled with media support)
+- [GnuTLS](https://www.gnutls.org/) or [OpenSSL](https://www.openssl.org/)
+- [usrsctp](https://github.com/sctplab/usrsctp) (as submodule by default)
+- [Plog](https://github.com/SergiusTheBest/plog) (as submodule by default)
+- [libjuice](https://github.com/paullouisageneau/libjuice) (as submodule by default) or [libnice](https://nice.freedesktop.org/) as an ICE backend.
+- [libsrtp](https://github.com/cisco/libsrtp) (as submodule by default) required if compiled with media support.
+- [Nlohmann JSON](https://github.com/nlohmann/json) (as submodule by default) required to build examples.
 
 ## Building
 

+ 51 - 0
cmake/Modules/FindUsrsctp.cmake

@@ -0,0 +1,51 @@
+#[=======================================================================[.rst
+FindUsrsctp
+----------
+
+FindModule for Usrsctp library
+
+Imported Targets
+^^^^^^^^^^^^^^^^
+
+This module defines the :prop_tgt:`IMPORTED` target ``Usrsctp::Usrsctp``.
+
+Result Variables
+^^^^^^^^^^^^^^^^
+
+This module sets the following variables:
+
+``Usrsctp_FOUND``
+  True, if the library was found.
+
+#]=======================================================================]
+
+include(FindPackageHandleStandardArgs)
+
+find_path(
+  Usrsctp_INCLUDE_DIR
+  NAMES usrsctp.h
+  PATHS /usr/include /usr/local/include)
+
+find_library(
+Usrsctp_LIBRARY
+  NAMES usrsctp libusrsctp
+  PATHS /usr/lib /usr/local/lib)
+
+find_package_handle_standard_args(
+  Usrsctp
+  REQUIRED_VARS Usrsctp_LIBRARY Usrsctp_INCLUDE_DIR)
+mark_as_advanced(Usrsctp_INCLUDE_DIR Usrsctp_LIBRARY)
+
+if(Usrsctp_FOUND)
+  if(NOT TARGET Usrsctp::Usrsctp)
+    if(IS_ABSOLUTE "${Usrsctp_LIBRARY}")
+      add_library(Usrsctp::Usrsctp UNKNOWN IMPORTED)
+      set_property(TARGET Usrsctp::Usrsctp PROPERTY IMPORTED_LOCATION "${Usrsctp_LIBRARY}")
+    else()
+      add_library(Usrsctp::Usrsctp INTERFACE IMPORTED)
+      set_property(TARGET Usrsctp::Usrsctp PROPERTY IMPORTED_LIBNAME "${Usrsctp_LIBRARY}")
+    endif()
+
+    set_target_properties(Usrsctp::Usrsctp PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${Usrsctp_INCLUDE_DIR}")
+  endif()
+endif()

+ 47 - 0
cmake/Modules/Findplog.cmake

@@ -0,0 +1,47 @@
+#[=======================================================================[.rst
+Findplog
+----------
+
+FindModule for Plog library
+
+Imported Targets
+^^^^^^^^^^^^^^^^
+
+This module defines the :prop_tgt:`IMPORTED` target ``plog::plog``.
+
+Result Variables
+^^^^^^^^^^^^^^^^
+
+This module sets the following variables:
+
+``plog_FOUND``
+  True, if the library was found.
+
+Cache variables
+^^^^^^^^^^^^^^^
+
+The following cache variables may also be set:
+
+``plog_INCLUDE_DIR``
+  Directory containing ``plog/Log.h``.
+
+#]=======================================================================]
+
+include(FindPackageHandleStandardArgs)
+
+find_path(
+  plog_INCLUDE_DIR
+  NAMES plog/Log.h
+  PATHS /usr/include /usr/local/include)
+
+find_package_handle_standard_args(
+  plog
+  REQUIRED_VARS plog_INCLUDE_DIR)
+mark_as_advanced(plog_INCLUDE_DIR)
+
+if(plog_FOUND)
+  if(NOT TARGET plog::plog)
+    add_library(plog::plog INTERFACE IMPORTED)
+    set_target_properties(plog::plog PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${plog_INCLUDE_DIR}")
+  endif()
+endif()

+ 1 - 1
examples/client-benchmark/CMakeLists.txt

@@ -29,7 +29,7 @@ set_target_properties(datachannel-client-benchmark PROPERTIES
 	XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.github.paullouisageneau.libdatachannel.examples.client.benchmark)
 
 find_package(Threads REQUIRED)
-target_link_libraries(datachannel-client-benchmark LibDataChannel::LibDataChannel Threads::Threads nlohmann_json)
+target_link_libraries(datachannel-client-benchmark LibDataChannel::LibDataChannel Threads::Threads nlohmann_json::nlohmann_json)
 
 if(MSVC)
 	add_custom_command(TARGET datachannel-client-benchmark POST_BUILD

+ 1 - 1
examples/client/CMakeLists.txt

@@ -43,7 +43,7 @@ set_target_properties(datachannel-client PROPERTIES
 	XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.github.paullouisageneau.libdatachannel.examples.client)
 
 find_package(Threads REQUIRED)
-target_link_libraries(datachannel-client LibDataChannel::LibDataChannel Threads::Threads nlohmann_json)
+target_link_libraries(datachannel-client LibDataChannel::LibDataChannel Threads::Threads nlohmann_json::nlohmann_json)
 
 if(MSVC)
 	add_custom_command(TARGET datachannel-client POST_BUILD

+ 1 - 1
examples/media-receiver/CMakeLists.txt

@@ -24,7 +24,7 @@ set_target_properties(datachannel-media-receiver PROPERTIES
 	XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.github.paullouisageneau.libdatachannel.examples.media-receiver)
 
 find_package(Threads REQUIRED)
-target_link_libraries(datachannel-media-receiver LibDataChannel::LibDataChannel Threads::Threads nlohmann_json)
+target_link_libraries(datachannel-media-receiver LibDataChannel::LibDataChannel Threads::Threads nlohmann_json::nlohmann_json)
 
 if(MSVC)
 	add_custom_command(TARGET datachannel-media-receiver POST_BUILD

+ 1 - 1
examples/media-sender/CMakeLists.txt

@@ -24,7 +24,7 @@ set_target_properties(datachannel-media-sender PROPERTIES
 	XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.github.paullouisageneau.libdatachannel.examples.media-sender)
 
 find_package(Threads REQUIRED)
-target_link_libraries(datachannel-media-sender LibDataChannel::LibDataChannel Threads::Threads nlohmann_json)
+target_link_libraries(datachannel-media-sender LibDataChannel::LibDataChannel Threads::Threads nlohmann_json::nlohmann_json)
 
 if(MSVC)
 	add_custom_command(TARGET datachannel-media-sender POST_BUILD

+ 1 - 1
examples/media-sfu/CMakeLists.txt

@@ -23,7 +23,7 @@ set_target_properties(datachannel-media-sfu PROPERTIES
 set_target_properties(datachannel-media-sfu PROPERTIES
 	XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.github.paullouisageneau.libdatachannel.examples.sfumedia)
 
-target_link_libraries(datachannel-media-sfu LibDataChannel::LibDataChannel nlohmann_json)
+target_link_libraries(datachannel-media-sfu LibDataChannel::LibDataChannel nlohmann_json::nlohmann_json)
 
 if(MSVC)
 	add_custom_command(TARGET datachannel-media-sfu POST_BUILD

+ 1 - 1
examples/streamer/CMakeLists.txt

@@ -45,7 +45,7 @@ set_target_properties(streamer PROPERTIES
 	XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.github.paullouisageneau.libdatachannel.examples.streamer)
 
 find_package(Threads REQUIRED)
-target_link_libraries(streamer LibDataChannel::LibDataChannel Threads::Threads nlohmann_json)
+target_link_libraries(streamer LibDataChannel::LibDataChannel Threads::Threads nlohmann_json::nlohmann_json)
 
 if(MSVC)
 	add_custom_command(TARGET streamer POST_BUILD

+ 38 - 25
include/rtc/nalunit.hpp

@@ -23,9 +23,9 @@ namespace rtc {
 struct RTC_CPP_EXPORT NalUnitHeader {
 	uint8_t _first = 0;
 
-	bool forbiddenBit() { return _first >> 7; }
-	uint8_t nri() { return _first >> 5 & 0x03; }
-	uint8_t unitType() { return _first & 0x1F; }
+	bool forbiddenBit() const { return _first >> 7; }
+	uint8_t nri() const { return _first >> 5 & 0x03; }
+	uint8_t unitType() const { return _first & 0x1F; }
 
 	void setForbiddenBit(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
 	void setNRI(uint8_t nri) { _first = (_first & 0x9F) | ((nri & 0x03) << 5); }
@@ -36,10 +36,10 @@ struct RTC_CPP_EXPORT NalUnitHeader {
 struct RTC_CPP_EXPORT NalUnitFragmentHeader {
 	uint8_t _first = 0;
 
-	bool isStart() { return _first >> 7; }
-	bool reservedBit6() { return (_first >> 5) & 0x01; }
-	bool isEnd() { return (_first >> 6) & 0x01; }
-	uint8_t unitType() { return _first & 0x1F; }
+	bool isStart() const { return _first >> 7; }
+	bool reservedBit6() const { return (_first >> 5) & 0x01; }
+	bool isEnd() const { return (_first >> 6) & 0x01; }
+	uint8_t unitType() const { return _first & 0x1F; }
 
 	void setStart(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
 	void setEnd(bool isSet) { _first = (_first & 0xBF) | (isSet << 6); }
@@ -53,17 +53,15 @@ struct RTC_CPP_EXPORT NalUnitFragmentHeader {
 struct RTC_CPP_EXPORT NalUnit : binary {
 	NalUnit(const NalUnit &unit) = default;
 	NalUnit(size_t size, bool includingHeader = true) : binary(size + (includingHeader ? 0 : 1)) {}
-
-	template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
-
 	NalUnit(binary &&data) : binary(std::move(data)) {}
-
 	NalUnit() : binary(1) {}
+	template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
+
+	bool forbiddenBit() const { return header()->forbiddenBit(); }
+	uint8_t nri() const { return header()->nri(); }
+	uint8_t unitType() const { return header()->unitType(); }
 
-	bool forbiddenBit() { return header()->forbiddenBit(); }
-	uint8_t nri() { return header()->nri(); }
-	uint8_t unitType() { return header()->unitType(); }
-	binary payload() {
+	binary payload() const {
 		assert(size() >= 1);
 		return {begin() + 1, end()};
 	}
@@ -71,6 +69,7 @@ struct RTC_CPP_EXPORT NalUnit : binary {
 	void setForbiddenBit(bool isSet) { header()->setForbiddenBit(isSet); }
 	void setNRI(uint8_t nri) { header()->setNRI(nri); }
 	void setUnitType(uint8_t type) { header()->setUnitType(type); }
+
 	void setPayload(binary payload) {
 		assert(size() >= 1);
 		erase(begin() + 1, end());
@@ -78,30 +77,35 @@ struct RTC_CPP_EXPORT NalUnit : binary {
 	}
 
 protected:
+	const NalUnitHeader *header() const {
+		assert(size() >= 1);
+		return reinterpret_cast<const NalUnitHeader *>(data());
+	}
+
 	NalUnitHeader *header() {
 		assert(size() >= 1);
-		return (NalUnitHeader *)data();
+		return reinterpret_cast<NalUnitHeader *>(data());
 	}
 };
 
 /// Nal unit fragment A
 struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit {
+	static std::vector<shared_ptr<NalUnitFragmentA>> fragmentsFrom(shared_ptr<NalUnit> nalu,
+	                                                               uint16_t maximumFragmentSize);
+
 	enum class FragmentType { Start, Middle, End };
 
 	NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType,
 	                 binary data);
 
-	static std::vector<shared_ptr<NalUnitFragmentA>> fragmentsFrom(shared_ptr<NalUnit> nalu,
-	                                                               uint16_t maximumFragmentSize);
-
-	uint8_t unitType() { return fragmentHeader()->unitType(); }
+	uint8_t unitType() const { return fragmentHeader()->unitType(); }
 
-	binary payload() {
+	binary payload() const {
 		assert(size() >= 2);
 		return {begin() + 2, end()};
 	}
 
-	FragmentType type() {
+	FragmentType type() const {
 		if (fragmentHeader()->isStart()) {
 			return FragmentType::Start;
 		} else if (fragmentHeader()->isEnd()) {
@@ -122,19 +126,28 @@ struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit {
 	void setFragmentType(FragmentType type);
 
 protected:
-	NalUnitHeader *fragmentIndicator() { return (NalUnitHeader *)data(); }
+	const uint8_t nal_type_fu_A = 28;
+
+	NalUnitHeader *fragmentIndicator() { return reinterpret_cast<NalUnitHeader *>(data()); }
+
+	const NalUnitHeader *fragmentIndicator() const {
+		return reinterpret_cast<const NalUnitHeader *>(data());
+	}
 
 	NalUnitFragmentHeader *fragmentHeader() {
-		return (NalUnitFragmentHeader *)fragmentIndicator() + 1;
+		return reinterpret_cast<NalUnitFragmentHeader *>(fragmentIndicator() + 1);
 	}
 
-	const uint8_t nal_type_fu_A = 28;
+	const NalUnitFragmentHeader *fragmentHeader() const {
+		return reinterpret_cast<const NalUnitFragmentHeader *>(fragmentIndicator() + 1);
+	}
 };
 
 class RTC_CPP_EXPORT NalUnits : public std::vector<shared_ptr<NalUnit>> {
 public:
 	static const uint16_t defaultMaximumFragmentSize =
 	    uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6
+
 	std::vector<shared_ptr<binary>> generateFragments(uint16_t maximumFragmentSize);
 };
 

+ 1 - 1
src/impl/dtlstransport.cpp

@@ -261,7 +261,7 @@ void DtlsTransport::doRecv() {
 		PLOG_ERROR << "DTLS recv: " << e.what();
 	}
 
-	gnutls_bye(mSession, GNUTLS_SHUT_RDWR);
+	gnutls_bye(mSession, GNUTLS_SHUT_WR);
 
 	PLOG_INFO << "DTLS closed";
 	changeState(State::Disconnected);

+ 4 - 0
src/impl/tcptransport.cpp

@@ -384,6 +384,10 @@ void TcpTransport::triggerBufferedAmount(size_t amount) {
 }
 
 void TcpTransport::process(PollService::Event event) {
+	auto self = weak_from_this().lock();
+	if (!self)
+		return;
+
 	try {
 		switch (event) {
 		case PollService::Event::Error: {

+ 1 - 1
src/impl/tlstransport.cpp

@@ -208,7 +208,7 @@ void TlsTransport::doRecv() {
 		PLOG_ERROR << "TLS recv: " << e.what();
 	}
 
-	gnutls_bye(mSession, GNUTLS_SHUT_RDWR);
+	gnutls_bye(mSession, GNUTLS_SHUT_WR);
 
 	PLOG_INFO << "TLS closed";
 	changeState(State::Disconnected);

+ 4 - 0
test/main.cpp

@@ -47,6 +47,9 @@ int main(int argc, char **argv) {
 		cerr << "WebRTC connectivity test failed: " << e.what() << endl;
 		return -1;
 	}
+
+// TODO: Temporarily disabled as the Open Relay TURN server is unreliable
+/*
 	try {
 		cout << endl << "*** Running WebRTC TURN connectivity test..." << endl;
 		test_turn_connectivity();
@@ -55,6 +58,7 @@ int main(int argc, char **argv) {
 		cerr << "WebRTC TURN connectivity test failed: " << e.what() << endl;
 		return -1;
 	}
+*/
 	try {
 		cout << endl << "*** Running WebRTC negotiated DataChannel test..." << endl;
 		test_negotiated();