Skip to content

Commit 5ec60aa

Browse files
committed
libpeer glue code
1 parent d54021e commit 5ec60aa

File tree

8 files changed

+215
-311
lines changed

8 files changed

+215
-311
lines changed

examples/libpeer/.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: Google

examples/libpeer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ find_package(peer REQUIRED)
2222

2323
set(src
2424
src/main.cpp
25-
src/webrtc_connection.cpp
2625
src/lws_http_client.cpp
2726
src/lws_websocket.cpp
2827
src/lws_context_manager.cpp
28+
src/libpeer_connection.cpp
2929
)
3030

3131
add_executable(libpeer_device "${src}")
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include "libpeer_connection.hpp"
2+
3+
#include <nabto/webrtc/util/logging.hpp>
4+
5+
namespace nabto::example {
6+
7+
namespace nrtc = nabto::webrtc;
8+
9+
std::atomic<bool> WebrtcConnection::isLibPeerInitialized_ = false;
10+
11+
PeerConnectionPtr WebrtcConnection::create(
12+
nrtc::SignalingDevicePtr device, nrtc::SignalingChannelPtr channel,
13+
nrtc::util::MessageTransportPtr transport) {
14+
if (!isLibPeerInitialized_) {
15+
isLibPeerInitialized_ = true;
16+
peer_init();
17+
}
18+
19+
auto conn = std::make_shared<WebrtcConnection>(device, channel, transport);
20+
conn->init();
21+
conn->pcConfig_ = {};
22+
return conn;
23+
}
24+
25+
WebrtcConnection::WebrtcConnection(nrtc::SignalingDevicePtr device,
26+
nrtc::SignalingChannelPtr channel,
27+
nrtc::util::MessageTransportPtr transport)
28+
: device_(device), channel_(channel), transport_(transport) {}
29+
30+
void WebrtcConnection::init() {
31+
auto self = shared_from_this();
32+
33+
transport_->addMessageListener(
34+
[self](nrtc::util::WebrtcSignalingMessage& msg) {
35+
NPLOGI << "Webrtc signaling message received";
36+
self->handleMessage(msg);
37+
});
38+
39+
transport_->addSetupDoneListener(
40+
[self](const std::vector<nrtc::IceServer>& iceServers) {
41+
NPLOGI << "Transport setup DONE";
42+
int n = 0;
43+
for (auto& iceServer : iceServers) {
44+
auto pcIceServer = &self->pcConfig_.ice_servers[n++];
45+
pcIceServer->urls = iceServer.urls[0].c_str();
46+
pcIceServer->username = iceServer.username.c_str();
47+
pcIceServer->credential = iceServer.credential.c_str();
48+
}
49+
self->createPeerConnection();
50+
});
51+
52+
transport_->addErrorListener([self](const nrtc::SignalingError& error) {
53+
NPLOGE << "Nabto signaling error: " << error.errorCode();
54+
self->handleTransportError(error);
55+
});
56+
57+
channel_->addStateChangeListener([self](nrtc::SignalingChannelState event) {
58+
NPLOGI << "SignalingChannelState changed";
59+
self->handleChannelStateChange(event);
60+
});
61+
62+
channel_->addErrorListener([self](const nrtc::SignalingError& error) {
63+
NPLOGE << "Channel error: " << error.errorCode();
64+
self->handleChannelError(error);
65+
});
66+
}
67+
68+
void WebrtcConnection::createPeerConnection() {
69+
NPLOGI << "Creating LibPeer connection...";
70+
71+
pcConfig_.datachannel = DATA_CHANNEL_NONE;
72+
pcConfig_.video_codec = CODEC_H264;
73+
pcConfig_.audio_codec = CODEC_NONE;
74+
pcConfig_.user_data = this;
75+
76+
pc_ = peer_connection_create(&pcConfig_);
77+
peer_connection_oniceconnectionstatechange(pc_, &WebrtcConnection::onIceConnectionStateChange);
78+
peer_connection_onicecandidate(pc_, &WebrtcConnection::onIceCandidate);
79+
80+
const char* offer = peer_connection_create_offer(pc_);
81+
NPLOGI << "OFFER: " << offer;
82+
sendDescription(offer, SDP_TYPE_OFFER);
83+
84+
if (pc_) {
85+
running = true;
86+
connectionTaskThread_ = std::thread(&WebrtcConnection::connectionLoop, this);
87+
}
88+
}
89+
90+
void WebrtcConnection::connectionLoop() {
91+
while (running) {
92+
peer_connection_loop(pc_);
93+
usleep(1000);
94+
}
95+
}
96+
97+
void WebrtcConnection::sendDescription(const char* description, SdpType type) {
98+
std::string typeString = type == SDP_TYPE_ANSWER ? "answer" : "offer";
99+
if (description) {
100+
nrtc::util::SignalingDescription desc(typeString, std::string(description));
101+
sendSignalingMessage(nrtc::util::WebrtcSignalingMessage(desc));
102+
}
103+
}
104+
105+
void WebrtcConnection::sendSignalingMessage(const nrtc::util::WebrtcSignalingMessage& message) {
106+
if (!transport_) {
107+
return;
108+
}
109+
110+
try {
111+
transport_->sendMessage(message);
112+
} catch (std::exception& e) {
113+
NPLOGE << "Failed to sign the message with error: " << e.what();
114+
}
115+
}
116+
117+
void WebrtcConnection::handleMessage(nrtc::util::WebrtcSignalingMessage& msg) {
118+
std::lock_guard<std::mutex> lock(mutex_);
119+
if (msg.isDescription()) {
120+
auto desc = msg.getDescription();
121+
NPLOGI << "Description received: " << desc.sdp;
122+
}
123+
}
124+
125+
void WebrtcConnection::handleTransportError(const nrtc::SignalingError& error) {
126+
127+
}
128+
129+
void WebrtcConnection::handleChannelStateChange(
130+
const nrtc::SignalingChannelState& state) {}
131+
132+
void WebrtcConnection::handleChannelError(const nrtc::SignalingError& error) {}
133+
134+
void WebrtcConnection::onIceConnectionStateChange(PeerConnectionState state,
135+
void* userdata) {
136+
NPLOGI << "ICE Connection state changed to " << state;
137+
}
138+
139+
void WebrtcConnection::onIceCandidate(char* description, void* userdata) {
140+
141+
}
142+
143+
} // namespace nabto::example
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
#include <memory>
3+
#include <mutex>
4+
#include <peer.h>
5+
#include <nabto/webrtc/device.hpp>
6+
#include <nabto/webrtc/util/message_transport.hpp>
7+
8+
namespace nabto::example {
9+
10+
class WebrtcConnection;
11+
typedef std::shared_ptr<WebrtcConnection> PeerConnectionPtr;
12+
13+
class WebrtcConnection : public std::enable_shared_from_this<WebrtcConnection> {
14+
public:
15+
static PeerConnectionPtr create(
16+
nabto::webrtc::SignalingDevicePtr device,
17+
nabto::webrtc::SignalingChannelPtr channel,
18+
nabto::webrtc::util::MessageTransportPtr transport);
19+
20+
WebrtcConnection(nabto::webrtc::SignalingDevicePtr device,
21+
nabto::webrtc::SignalingChannelPtr channel,
22+
nabto::webrtc::util::MessageTransportPtr transport);
23+
24+
private:
25+
static std::atomic<bool> isLibPeerInitialized_;
26+
27+
static void onIceConnectionStateChange(PeerConnectionState state, void* userdata);
28+
static void onIceCandidate(char* description, void* userdata);
29+
static void onMessage(char* msg, size_t len, void* userdata, uint16_t sid);
30+
31+
void init();
32+
void connectionLoop();
33+
void createPeerConnection();
34+
35+
void sendDescription(const char* desc, SdpType type);
36+
void sendSignalingMessage(const nabto::webrtc::util::WebrtcSignalingMessage& message);
37+
38+
void handleMessage(nabto::webrtc::util::WebrtcSignalingMessage& msg);
39+
void handleTransportError(const nabto::webrtc::SignalingError& error);
40+
void handleChannelStateChange(const nabto::webrtc::SignalingChannelState& state);
41+
void handleChannelError(const nabto::webrtc::SignalingError& error);
42+
43+
nabto::webrtc::SignalingChannelPtr channel_;
44+
nabto::webrtc::SignalingDevicePtr device_;
45+
nabto::webrtc::util::MessageTransportPtr transport_;
46+
47+
PeerConnection* pc_;
48+
PeerConnectionState pcState_;
49+
PeerConfiguration pcConfig_;
50+
bool running;
51+
52+
std::thread connectionTaskThread_;
53+
std::mutex mutex_;
54+
};
55+
} // namespace nabto::example

examples/libpeer/src/lws_http_client.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ bool LwsHttpClient::sendRequest(const nabto::webrtc::SignalingHttpRequest& reque
4242

4343
uint64_t handle = requestIndex_++;
4444
requests_[handle] = { this, cb, request, "", 0, 0 };
45-
requests_[handle].req.headers.push_back({ "Content-Length", std::to_string(request.body.length()) });
45+
46+
if (request.method == "POST" && request.body.length() > 0)
47+
{
48+
requests_[handle].req.headers.push_back({ "Content-Length", std::to_string(request.body.length()) });
49+
}
4650

4751
ccinfo.protocol = "lws-http-protocol";
4852
ccinfo.userdata = reinterpret_cast<void*>(handle);

examples/libpeer/src/main.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "lws_websocket.hpp"
1515
#include "lws_http_client.hpp"
16-
#include "webrtc_connection.hpp"
16+
#include "libpeer_connection.hpp"
1717

1818
#include "deviceconf.hpp"
1919

@@ -48,6 +48,15 @@ int main() {
4848

4949
device->addNewChannelListener([device](nabto::webrtc::SignalingChannelPtr channel, bool authorized) {
5050
NPLOGI << "New channel!";
51+
auto transport = nabto::webrtc::util::MessageTransportFactory::createSharedSecretTransport(
52+
device,
53+
channel,
54+
[](const std::string keyId) -> std::string {
55+
return sharedSecret;
56+
}
57+
);
58+
59+
auto conn = nabto::example::WebrtcConnection::create(device, channel, transport);
5160
});
5261

5362
device->start();

0 commit comments

Comments
 (0)