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
0 commit comments