|
19 | 19 | #include "impl/init.hpp"
|
20 | 20 |
|
21 | 21 | #include <mutex>
|
| 22 | +#include <map> |
| 23 | + |
| 24 | +#if !USE_NICE |
| 25 | +#include <juice/juice.h> |
| 26 | +#endif |
22 | 27 |
|
23 | 28 | namespace {
|
24 | 29 |
|
@@ -88,6 +93,54 @@ std::shared_future<void> Cleanup() { return impl::Init::Instance().cleanup(); }
|
88 | 93 |
|
89 | 94 | void SetSctpSettings(SctpSettings s) { impl::Init::Instance().setSctpSettings(std::move(s)); }
|
90 | 95 |
|
| 96 | +#if !USE_NICE |
| 97 | + |
| 98 | +UnhandledStunRequestCallback unboundStunCallback; |
| 99 | + |
| 100 | +void InvokeUnhandledStunRequestCallback (const juice_mux_binding_request *info, void *user_ptr) { |
| 101 | + PLOG_DEBUG << "Invoking Unbind STUN listener"; |
| 102 | + auto callback = static_cast<UnhandledStunRequestCallback *>(user_ptr); |
| 103 | + |
| 104 | + (*callback)({ |
| 105 | + std::string(info->local_ufrag), |
| 106 | + std::string(info->remote_ufrag), |
| 107 | + std::string(info->address), |
| 108 | + info->port |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +#endif |
| 113 | + |
| 114 | +void OnUnhandledStunRequest ([[maybe_unused]] std::string host, [[maybe_unused]] int port, [[maybe_unused]] UnhandledStunRequestCallback callback) { |
| 115 | + #if USE_NICE |
| 116 | + PLOG_WARNING << "BindStunListener is not supported with libnice, please use libjuice"; |
| 117 | + #else |
| 118 | + if (callback == NULL) { |
| 119 | + PLOG_DEBUG << "Removing unhandled STUN request listener"; |
| 120 | + |
| 121 | + // call with NULL callback to unbind |
| 122 | + if (juice_mux_listen(host.c_str(), port, NULL, NULL) < 0) { |
| 123 | + throw std::runtime_error("Could not unbind STUN listener"); |
| 124 | + } |
| 125 | + unboundStunCallback = NULL; |
| 126 | + |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + PLOG_DEBUG << "Adding listener for unhandled STUN requests"; |
| 131 | + |
| 132 | + if (unboundStunCallback != NULL) { |
| 133 | + throw std::runtime_error("Unhandled STUN request handler already present"); |
| 134 | + } |
| 135 | + |
| 136 | + unboundStunCallback = std::move(callback); |
| 137 | + |
| 138 | + if (juice_mux_listen(host.c_str(), port, &InvokeUnhandledStunRequestCallback, &unboundStunCallback) < 0) { |
| 139 | + throw std::invalid_argument("Could add listener for unhandled STUN requests"); |
| 140 | + } |
| 141 | + #endif |
| 142 | +} |
| 143 | + |
91 | 144 | RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, LogLevel level) {
|
92 | 145 | switch (level) {
|
93 | 146 | case LogLevel::Fatal:
|
|
0 commit comments