Skip to content

Commit 4af012f

Browse files
authored
Merge pull request #11 from nabto/qa
Qa
2 parents bd26fcb + bc3b637 commit 4af012f

File tree

10 files changed

+78
-19
lines changed

10 files changed

+78
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ Run the RTSP device:
6060
-k key.pem --secret <secret> \
6161
-r rtsp://127.0.0.1:8554/video`
6262
```
63+
64+
## Limitations
65+
66+
The libdatachannel WebRTC library used by the examples does not currently support offers generated with `restartIce()` due to this [issue](https://github.com/paullouisageneau/libdatachannel/issues/545). This means if one of the peers experiences a network failure or switches to a different network, any open WebRTC connections cannot be renegotiated. Instead the connection has to be recreated from scratch.

examples/libdatachannel/src/common/webrtc_connection/webrtc_connection.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ void WebrtcConnection::handleMessage(const nlohmann::json& msg) {
109109
"discarding the received offer";
110110
return;
111111
}
112-
113-
pc_->setRemoteDescription(remDesc);
112+
try {
113+
pc_->setRemoteDescription(remDesc);
114+
} catch (std::exception& ex) {
115+
NPLOGE << "Failed to set remote description: " << remDesc.generateSdp()
116+
<< " Failed with exception: " << ex.what();
117+
pc_->close();
118+
}
114119
} else if (type == "CANDIDATE") {
115120
try {
116121
rtc::Candidate cand(
@@ -195,7 +200,7 @@ void WebrtcConnection::handleStateChange(
195200
// \o/ we should use the connection
196201
break;
197202
case rtc::PeerConnection::State::Failed:
198-
// connection fail and we should fix it
203+
// connection failed
199204
case rtc::PeerConnection::State::Closed:
200205
// connection closed we should clean up
201206
NPLOGI << "Webrtc Connection " << state;

sdk/src/signaling_device/include/nabto/webrtc/device.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,18 @@ class SignalingTimer {
293293
SignalingTimer& operator=(SignalingTimer&&) = delete;
294294

295295
/**
296-
* Set a timeout in ms at which the callback sould be invoked.
296+
* Set a timeout in ms at which the callback should be invoked.
297297
*
298298
* @param timeoutMs The timeout in milliseconds.
299299
* @param callback The callback to be invoked once the timeout has passed.
300300
*/
301301
virtual void setTimeout(uint32_t timeoutMs,
302302
std::function<void()> callback) = 0;
303+
304+
/**
305+
* Cancel a started timer.
306+
*/
307+
virtual void cancel() = 0;
303308
};
304309

305310
/**

sdk/src/signaling_device/src/signaling_device_impl.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,29 @@ void SignalingDeviceImpl::close() {
171171
{
172172
const std::lock_guard<std::mutex> lock(mutex_);
173173
channels_.clear();
174-
chanHandlers_.clear();
175-
stateHandlers_.clear();
176-
reconnHandlers_.clear();
177174

178175
if (ws_) {
179176
ws_->close();
180177
}
181178
}
179+
if (!ws_) {
180+
deinit();
181+
}
182+
}
183+
184+
void SignalingDeviceImpl::deinit() {
185+
SignalingTimerPtr timer = nullptr;
186+
{
187+
const std::lock_guard<std::mutex> lock(mutex_);
188+
timer = timer_;
189+
}
190+
if (timer) {
191+
timer->cancel();
192+
}
193+
const std::lock_guard<std::mutex> lock(mutex_);
194+
chanHandlers_.clear();
195+
stateHandlers_.clear();
196+
reconnHandlers_.clear();
182197
}
183198

184199
void SignalingDeviceImpl::connectWs() {
@@ -212,6 +227,8 @@ void SignalingDeviceImpl::connectWs() {
212227
if (self->state_ != SignalingDeviceState::CLOSED &&
213228
self->state_ != SignalingDeviceState::FAILED) {
214229
self->waitReconnect();
230+
} else {
231+
self->deinit();
215232
}
216233
});
217234

@@ -220,6 +237,8 @@ void SignalingDeviceImpl::connectWs() {
220237
if (self->state_ != SignalingDeviceState::CLOSED &&
221238
self->state_ != SignalingDeviceState::FAILED) {
222239
self->waitReconnect();
240+
} else {
241+
self->deinit();
223242
}
224243
});
225244

sdk/src/signaling_device/src/signaling_device_impl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class SignalingDeviceImpl
110110
SignalingDeviceState state_ = SignalingDeviceState::NEW;
111111
std::mutex mutex_;
112112

113+
void deinit();
114+
113115
// WS STUFF
114116
WebsocketConnectionPtr ws_;
115117
size_t reconnectCounter_ = 0;

sdk/src/signaling_device/src/websocket_connection.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ class WebsocketConnection
3030
SignalingTimerFactoryPtr timerFactory)
3131
: ws_(std::move(websocket)), timerFactory_(std::move(timerFactory)) {}
3232

33+
~WebsocketConnection() {
34+
if (timer_) {
35+
timer_->cancel();
36+
}
37+
}
38+
39+
WebsocketConnection() = default;
40+
WebsocketConnection(const WebsocketConnection&) = delete;
41+
WebsocketConnection& operator=(const WebsocketConnection&) = delete;
42+
WebsocketConnection(WebsocketConnection&&) = delete;
43+
WebsocketConnection& operator=(WebsocketConnection&&) = delete;
44+
3345
bool send(const std::string& data);
3446
void close();
3547
void onOpen(std::function<void()> callback);
@@ -46,7 +58,7 @@ class WebsocketConnection
4658
SignalingWebsocketPtr ws_;
4759
SignalingTimerFactoryPtr timerFactory_;
4860
size_t pongCounter_ = 0;
49-
SignalingTimerPtr timer_;
61+
SignalingTimerPtr timer_ = nullptr;
5062

5163
void handlePong();
5264
static SignalingMessageType parseWsMsgType(const std::string& str);

sdk/src/signaling_util/curl_http_client/include/nabto/webrtc/util/curl_async.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class CurlHttpClient : public nabto::signaling::SignalingHttpClient,
3030
static nabto::signaling::SignalingHttpClientPtr create();
3131

3232
CurlHttpClient();
33+
~CurlHttpClient() override;
34+
CurlHttpClient(const CurlHttpClient&) = delete;
35+
CurlHttpClient& operator=(const CurlHttpClient&) = delete;
36+
CurlHttpClient(CurlHttpClient&&) = delete;
37+
CurlHttpClient& operator=(CurlHttpClient&&) = delete;
38+
3339
bool sendRequest(const nabto::signaling::SignalingHttpRequest& request,
3440
nabto::signaling::HttpResponseCallback cb) override;
3541

sdk/src/signaling_util/curl_http_client/src/curl_async.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ nabto::signaling::SignalingHttpClientPtr CurlHttpClient::create() {
2323

2424
CurlHttpClient::CurlHttpClient() : curl_(CurlAsync::create()) {}
2525

26+
CurlHttpClient::~CurlHttpClient() {
27+
if (curlReqHeaders_ != nullptr) {
28+
curl_slist_free_all(curlReqHeaders_);
29+
curlReqHeaders_ = nullptr;
30+
}
31+
}
32+
2633
bool CurlHttpClient::sendRequest(
2734
const nabto::signaling::SignalingHttpRequest& request,
2835
nabto::signaling::HttpResponseCallback cb) {
@@ -157,10 +164,8 @@ CurlAsyncPtr CurlAsync::create() {
157164
}
158165

159166
CurlAsync::~CurlAsync() {
160-
try {
161-
thread_ = std::thread();
162-
} catch (std::exception& ex) {
163-
NPLOGE << "~CurlAsync exception " << ex.what();
167+
if (!stopped_) {
168+
stop();
164169
}
165170
curl_easy_cleanup(curl_);
166171
curl_global_cleanup();

sdk/src/signaling_util/message_transport/src/message_transport.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <nabto/webrtc/device.hpp>
44
#include <nabto/webrtc/util/message_transport.hpp>
55

6-
#include <functional>
7-
#include <string>
86
#include <utility>
97

108
namespace nabto {

sdk/src/signaling_util/std_timer/include/nabto/webrtc/util/std_timer.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ namespace util {
1515
class StdTimer : public nabto::signaling::SignalingTimer,
1616
public std::enable_shared_from_this<StdTimer> {
1717
public:
18-
~StdTimer() {
19-
if (timer_.joinable()) {
20-
timer_.join();
21-
}
22-
}
18+
~StdTimer() {}
2319

2420
void setTimeout(uint32_t timeoutMs, std::function<void()> cb) override {
2521
auto self = shared_from_this();
@@ -30,6 +26,13 @@ class StdTimer : public nabto::signaling::SignalingTimer,
3026
});
3127
}
3228

29+
void cancel() override {
30+
if (timer_.joinable()) {
31+
timer_.join();
32+
timer_ = std::thread();
33+
}
34+
}
35+
3336
private:
3437
std::thread timer_;
3538
};

0 commit comments

Comments
 (0)