فهرست منبع

Split include.hpp

Paul-Louis Ageneau 4 سال پیش
والد
کامیت
54d0cb898d

+ 4 - 114
include/rtc/include.hpp

@@ -40,6 +40,7 @@
 #endif
 
 #include "log.hpp"
+#include "utils.hpp"
 
 #include <cstddef>
 #include <functional>
@@ -52,13 +53,13 @@
 
 namespace rtc {
 
-using std::nullopt;
 using std::byte;
+using std::nullopt;
+using std::shared_ptr;
 using std::string;
 using std::string_view;
-using std::shared_ptr;
-using std::weak_ptr;
 using std::unique_ptr;
+using std::weak_ptr;
 
 using binary = std::vector<byte>;
 using binary_ptr = std::shared_ptr<binary>;
@@ -69,117 +70,6 @@ using std::uint32_t;
 using std::uint64_t;
 using std::uint8_t;
 
-const size_t MAX_NUMERICNODE_LEN = 48; // Max IPv6 string representation length
-const size_t MAX_NUMERICSERV_LEN = 6;  // Max port string representation length
-
-const uint16_t DEFAULT_SCTP_PORT = 5000;          // SCTP port to use by default
-const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536;    // Remote max message size if not specified in SDP
-const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
-
-const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size
-
-const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool
-
-const size_t DEFAULT_IPV4_MTU = 1200; // IPv4 safe MTU value recommended by RFC 8261
-
-// overloaded helper
-template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
-template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
-
-// weak_ptr bind helper
-template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&..._args) {
-	return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&...args) {
-		using result_type = typename decltype(bound)::result_type;
-		if (auto shared_this = weak_this.lock())
-			return bound(args...);
-		else
-			return static_cast<result_type>(false);
-	};
-}
-
-// scope_guard helper
-class scope_guard {
-public:
-	scope_guard(std::function<void()> func) : function(std::move(func)) {}
-	scope_guard(scope_guard &&other) = delete;
-	scope_guard(const scope_guard &) = delete;
-	void operator=(const scope_guard &) = delete;
-
-	~scope_guard() {
-		if (function)
-			function();
-	}
-
-private:
-	std::function<void()> function;
-};
-
-// callback with built-in synchronization
-template <typename... Args> class synchronized_callback {
-public:
-	synchronized_callback() = default;
-	synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
-	synchronized_callback(const synchronized_callback &cb) { *this = cb; }
-	synchronized_callback(std::function<void(Args...)> func) { *this = std::move(func); }
-	~synchronized_callback() { *this = nullptr; }
-
-	synchronized_callback &operator=(synchronized_callback &&cb) {
-		std::scoped_lock lock(mutex, cb.mutex);
-		callback = std::move(cb.callback);
-		cb.callback = nullptr;
-		return *this;
-	}
-
-	synchronized_callback &operator=(const synchronized_callback &cb) {
-		std::scoped_lock lock(mutex, cb.mutex);
-		callback = cb.callback;
-		return *this;
-	}
-
-	synchronized_callback &operator=(std::function<void(Args...)> func) {
-		std::lock_guard lock(mutex);
-		callback = std::move(func);
-		return *this;
-	}
-
-	void operator()(Args... args) const {
-		std::lock_guard lock(mutex);
-		if (callback)
-			callback(std::move(args)...);
-	}
-
-	operator bool() const {
-		std::lock_guard lock(mutex);
-		return callback ? true : false;
-	}
-
-	std::function<void(Args...)> wrap() const {
-		return [this](Args... args) { (*this)(std::move(args)...); };
-	}
-
-private:
-	std::function<void(Args...)> callback;
-	mutable std::recursive_mutex mutex;
-};
-
-// pimpl base class
-template<typename T> using impl_ptr = std::shared_ptr<T>;
-template <typename T> class CheshireCat {
-public:
-	CheshireCat(impl_ptr<T> impl) : mImpl(std::move(impl)) {}
-	template <typename... Args>
-	CheshireCat(Args... args) : mImpl(std::make_shared<T>(std::move(args)...)) {}
-
-	virtual ~CheshireCat() = default;
-
-protected:
-	impl_ptr<T> impl() { return mImpl; }
-	impl_ptr<const T> impl() const { return mImpl; }
-
-private:
-	impl_ptr<T> mImpl;
-};
-
 } // namespace rtc
 
 #endif

+ 128 - 0
include/rtc/utils.hpp

@@ -0,0 +1,128 @@
+/**
+ * Copyright (c) 2019-2021 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_UTILS_H
+#define RTC_UTILS_H
+
+#include <functional>
+#include <memory>
+#include <mutex>
+
+namespace rtc {
+
+// overloaded helper
+template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
+template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
+
+// weak_ptr bind helper
+template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&..._args) {
+	return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&...args) {
+		using result_type = typename decltype(bound)::result_type;
+		if (auto shared_this = weak_this.lock())
+			return bound(args...);
+		else
+			return static_cast<result_type>(false);
+	};
+}
+
+// scope_guard helper
+class scope_guard {
+public:
+	scope_guard(std::function<void()> func) : function(std::move(func)) {}
+	scope_guard(scope_guard &&other) = delete;
+	scope_guard(const scope_guard &) = delete;
+	void operator=(const scope_guard &) = delete;
+
+	~scope_guard() {
+		if (function)
+			function();
+	}
+
+private:
+	std::function<void()> function;
+};
+
+// callback with built-in synchronization
+template <typename... Args> class synchronized_callback {
+public:
+	synchronized_callback() = default;
+	synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
+	synchronized_callback(const synchronized_callback &cb) { *this = cb; }
+	synchronized_callback(std::function<void(Args...)> func) { *this = std::move(func); }
+	~synchronized_callback() { *this = nullptr; }
+
+	synchronized_callback &operator=(synchronized_callback &&cb) {
+		std::scoped_lock lock(mutex, cb.mutex);
+		callback = std::move(cb.callback);
+		cb.callback = nullptr;
+		return *this;
+	}
+
+	synchronized_callback &operator=(const synchronized_callback &cb) {
+		std::scoped_lock lock(mutex, cb.mutex);
+		callback = cb.callback;
+		return *this;
+	}
+
+	synchronized_callback &operator=(std::function<void(Args...)> func) {
+		std::lock_guard lock(mutex);
+		callback = std::move(func);
+		return *this;
+	}
+
+	void operator()(Args... args) const {
+		std::lock_guard lock(mutex);
+		if (callback)
+			callback(std::move(args)...);
+	}
+
+	operator bool() const {
+		std::lock_guard lock(mutex);
+		return callback ? true : false;
+	}
+
+	std::function<void(Args...)> wrap() const {
+		return [this](Args... args) { (*this)(std::move(args)...); };
+	}
+
+private:
+	std::function<void(Args...)> callback;
+	mutable std::recursive_mutex mutex;
+};
+
+// pimpl base class
+template <typename T> using impl_ptr = std::shared_ptr<T>;
+template <typename T> class CheshireCat {
+public:
+	CheshireCat(impl_ptr<T> impl) : mImpl(std::move(impl)) {}
+	template <typename... Args>
+	CheshireCat(Args... args) : mImpl(std::make_shared<T>(std::move(args)...)) {}
+
+	virtual ~CheshireCat() = default;
+
+protected:
+	impl_ptr<T> impl() { return mImpl; }
+	impl_ptr<const T> impl() const { return mImpl; }
+
+private:
+	impl_ptr<T> mImpl;
+};
+
+} // namespace rtc
+
+#endif

+ 1 - 0
src/candidate.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "candidate.hpp"
+#include "globals.hpp"
 
 #include <algorithm>
 #include <array>

+ 0 - 2
src/capi.cpp

@@ -43,8 +43,6 @@
 
 using namespace rtc;
 using std::optional;
-using std::shared_ptr;
-using std::string;
 using std::chrono::milliseconds;
 
 namespace {

+ 1 - 0
src/channel.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "channel.hpp"
+#include "globals.hpp"
 
 #include "impl/channel.hpp"
 

+ 1 - 0
src/datachannel.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "datachannel.hpp"
+#include "globals.hpp"
 #include "include.hpp"
 #include "peerconnection.hpp"
 

+ 0 - 1
src/description.cpp

@@ -28,7 +28,6 @@
 #include <sstream>
 #include <unordered_map>
 
-using std::shared_ptr;
 using std::chrono::system_clock;
 
 namespace {

+ 41 - 0
src/globals.hpp

@@ -0,0 +1,41 @@
+/**
+ * Copyright (c) 2019-2021 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_GLOBALS_H
+#define RTC_GLOBALS_H
+
+#include "include.hpp"
+
+namespace rtc {
+
+const size_t MAX_NUMERICNODE_LEN = 48; // Max IPv6 string representation length
+const size_t MAX_NUMERICSERV_LEN = 6;  // Max port string representation length
+
+const uint16_t DEFAULT_SCTP_PORT = 5000;          // SCTP port to use by default
+const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536;    // Remote max message size if not specified in SDP
+const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
+
+const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size
+
+const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool (>= 2)
+
+const size_t DEFAULT_IPV4_MTU = 1200; // IPv4 safe MTU value recommended by RFC 8261
+
+} // namespace rtc
+
+#endif

+ 1 - 4
src/h264rtppacketizer.cpp

@@ -22,9 +22,6 @@
 
 namespace rtc {
 
-using std::make_shared;
-using std::shared_ptr;
-
 typedef enum {
 	NUSM_noMatch,
 	NUSM_firstZero,
@@ -74,7 +71,7 @@ NalUnitStartSequenceMatch StartSequenceMatchSucc(NalUnitStartSequenceMatch match
 }
 
 shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
-	auto nalus = make_shared<NalUnits>();
+	auto nalus = std::make_shared<NalUnits>();
 	if (separator == Separator::Length) {
 		unsigned long long index = 0;
 		while (index < message->size()) {

+ 1 - 0
src/impl/datachannel.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "datachannel.hpp"
+#include "globals.hpp"
 #include "include.hpp"
 #include "logcounter.hpp"
 #include "peerconnection.hpp"

+ 2 - 6
src/impl/dtlstransport.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "dtlstransport.hpp"
+#include "globals.hpp"
 #include "icetransport.hpp"
 
 #include <chrono>
@@ -34,11 +35,6 @@
 
 using namespace std::chrono;
 
-using std::shared_ptr;
-using std::string;
-using std::unique_ptr;
-using std::weak_ptr;
-
 namespace rtc::impl {
 
 #if USE_GNUTLS
@@ -597,4 +593,4 @@ long DtlsTransport::BioMethodCtrl(BIO * /*bio*/, int cmd, long /*num*/, void * /
 
 #endif
 
-} // namespace rtc
+} // namespace rtc::impl

+ 29 - 27
src/impl/peerconnection.cpp

@@ -20,6 +20,7 @@
 #include "peerconnection.hpp"
 #include "certificate.hpp"
 #include "dtlstransport.hpp"
+#include "globals.hpp"
 #include "icetransport.hpp"
 #include "include.hpp"
 #include "logcounter.hpp"
@@ -63,7 +64,8 @@ static LogCounter
                                 "Number of unknown RTCP packet types over past second");
 
 PeerConnection::PeerConnection(Configuration config_)
-    : config(std::move(config_)), mCertificate(make_certificate()), mProcessor(std::make_unique<Processor>()) {
+    : config(std::move(config_)), mCertificate(make_certificate()),
+      mProcessor(std::make_unique<Processor>()) {
 	PLOG_VERBOSE << "Creating PeerConnection";
 
 	if (config.portRangeEnd && config.portRangeBegin > config.portRangeEnd)
@@ -181,32 +183,32 @@ shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
 		auto certificate = mCertificate.get();
 		auto lower = std::atomic_load(&mIceTransport);
 		auto verifierCallback = weak_bind(&PeerConnection::checkFingerprint, this, _1);
-		auto stateChangeCallback = [this,
-		                            weak_this = weak_from_this()](DtlsTransport::State transportState) {
-			auto shared_this = weak_this.lock();
-			if (!shared_this)
-				return;
-
-			switch (transportState) {
-			case DtlsTransport::State::Connected:
-				if (auto remote = remoteDescription(); remote && remote->hasApplication())
-					initSctpTransport();
-				else
-					changeState(State::Connected);
-
-				mProcessor->enqueue(&PeerConnection::openTracks, this);
-				break;
-			case DtlsTransport::State::Failed:
-				changeState(State::Failed);
-				break;
-			case DtlsTransport::State::Disconnected:
-				changeState(State::Disconnected);
-				break;
-			default:
-				// Ignore
-				break;
-			}
-		};
+		auto stateChangeCallback =
+		    [this, weak_this = weak_from_this()](DtlsTransport::State transportState) {
+			    auto shared_this = weak_this.lock();
+			    if (!shared_this)
+				    return;
+
+			    switch (transportState) {
+			    case DtlsTransport::State::Connected:
+				    if (auto remote = remoteDescription(); remote && remote->hasApplication())
+					    initSctpTransport();
+				    else
+					    changeState(State::Connected);
+
+				    mProcessor->enqueue(&PeerConnection::openTracks, this);
+				    break;
+			    case DtlsTransport::State::Failed:
+				    changeState(State::Failed);
+				    break;
+			    case DtlsTransport::State::Disconnected:
+				    changeState(State::Disconnected);
+				    break;
+			    default:
+				    // Ignore
+				    break;
+			    }
+		    };
 
 		shared_ptr<DtlsTransport> transport;
 		if (auto local = localDescription(); local && local->hasAudioOrVideo()) {

+ 2 - 1
src/impl/sctptransport.cpp

@@ -18,6 +18,7 @@
 
 #include "sctptransport.hpp"
 #include "dtlstransport.hpp"
+#include "globals.hpp"
 #include "logcounter.hpp"
 
 #include <chrono>
@@ -808,4 +809,4 @@ int SctpTransport::WriteCallback(void *ptr, void *data, size_t len, uint8_t tos,
 	return transport->handleWrite(static_cast<byte *>(data), len, tos, set_df);
 }
 
-} // namespace rtc
+} // namespace rtc::impl

+ 1 - 0
src/impl/tcptransport.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "tcptransport.hpp"
+#include "globals.hpp"
 
 #if RTC_ENABLE_WEBSOCKET
 

+ 0 - 5
src/impl/tlstransport.cpp

@@ -28,11 +28,6 @@
 
 using namespace std::chrono;
 
-using std::shared_ptr;
-using std::string;
-using std::unique_ptr;
-using std::weak_ptr;
-
 namespace rtc::impl {
 
 #if USE_GNUTLS

+ 1 - 0
src/impl/track.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "track.hpp"
+#include "globals.hpp"
 #include "logcounter.hpp"
 
 namespace rtc::impl {

+ 0 - 5
src/impl/verifiedtlstransport.cpp

@@ -21,11 +21,6 @@
 
 #if RTC_ENABLE_WEBSOCKET
 
-using std::shared_ptr;
-using std::string;
-using std::unique_ptr;
-using std::weak_ptr;
-
 namespace rtc::impl {
 
 VerifiedTlsTransport::VerifiedTlsTransport(shared_ptr<TcpTransport> lower, string host,

+ 1 - 0
src/impl/websocket.cpp

@@ -19,6 +19,7 @@
 #if RTC_ENABLE_WEBSOCKET
 
 #include "websocket.hpp"
+#include "globals.hpp"
 #include "include.hpp"
 #include "threadpool.hpp"
 

+ 1 - 0
src/init.cpp

@@ -17,6 +17,7 @@
  */
 
 #include "init.hpp"
+#include "globals.hpp"
 
 #include "impl/certificate.hpp"
 #include "impl/dtlstransport.hpp"

+ 1 - 0
src/websocket.cpp

@@ -19,6 +19,7 @@
 #if RTC_ENABLE_WEBSOCKET
 
 #include "websocket.hpp"
+#include "globals.hpp"
 #include "include.hpp"
 
 #include "impl/websocket.hpp"