Skip to content

Commit b630236

Browse files
committed
refactor: null check created peer
1 parent 0e4125d commit b630236

File tree

6 files changed

+55
-28
lines changed

6 files changed

+55
-28
lines changed

src/conductor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ rtc::scoped_refptr<RtcPeer> Conductor::CreatePeerConnection(PeerConfig config) {
114114
}
115115

116116
config.timeout = args.peer_timeout;
117-
auto peer = RtcPeer::Create(std::move(config));
117+
auto peer = RtcPeer::Create(config);
118118
auto result = peer_connection_factory_->CreatePeerConnectionOrError(
119119
config, webrtc::PeerConnectionDependencies(peer.get()));
120120

src/rtc_peer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
struct PeerConfig : public webrtc::PeerConnectionInterface::RTCConfiguration {
1616
int timeout = 10;
1717
bool is_publisher = true;
18+
bool is_sfu_peer = false;
1819
bool has_candidates_in_sdp = false;
1920
};
2021

src/signaling/http_service.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ std::shared_ptr<HttpService> HttpService::Create(Args args, std::shared_ptr<Cond
1313

1414
HttpService::HttpService(Args args, std::shared_ptr<Conductor> conductor,
1515
boost::asio::io_context &ioc)
16-
: SignalingService(conductor, true),
16+
: SignalingService(conductor),
1717
port_(args.http_port),
1818
acceptor_({ioc, {boost::asio::ip::address_v6::any(), port_}}) {}
1919

@@ -108,7 +108,9 @@ void HttpSession::HandleRequest() {
108108

109109
void HttpSession::HandlePostRequest() {
110110
if (content_type_ == "application/sdp") {
111-
auto peer = http_service_->CreatePeer();
111+
PeerConfig config;
112+
config.has_candidates_in_sdp = true;
113+
auto peer = http_service_->CreatePeer(config);
112114
peer->OnLocalSdp([self = shared_from_this()](const std::string &peer_id,
113115
const std::string &sdp,
114116
const std::string &type) {

src/signaling/mqtt_service.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ void MqttService::OnMessage(struct mosquitto *mosq, void *obj,
152152

153153
if (topic.starts_with(sdp_base_topic_)) {
154154
auto peer = CreatePeer();
155+
if (!peer) {
156+
ERROR_PRINT("Failed to create peer.");
157+
return;
158+
}
159+
155160
peer->OnLocalSdp(
156161
[this](const std::string &peer_id, const std::string &sdp, const std::string &type) {
157162
AnswerLocalSdp(peer_id, sdp, type);

src/signaling/signaling_service.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
class SignalingService {
99
public:
1010
SignalingService(std::shared_ptr<Conductor> conductor, bool has_candidates_in_sdp = false)
11-
: conductor(conductor),
12-
has_candidates_in_sdp_(has_candidates_in_sdp) {}
11+
: conductor(conductor) {}
1312

1413
void Start() {
1514
worker_ = std::make_unique<Worker>("cleaner", [this]() {
@@ -21,10 +20,15 @@ class SignalingService {
2120
}
2221

2322
rtc::scoped_refptr<RtcPeer> CreatePeer(PeerConfig config = PeerConfig{}) {
24-
config.has_candidates_in_sdp = has_candidates_in_sdp_;
23+
if (!conductor) {
24+
ERROR_PRINT("Conductor is not initialized.");
25+
return nullptr;
26+
}
2527

26-
auto peer = conductor->CreatePeerConnection(std::move(config));
27-
peer_map_[peer->GetId()] = peer;
28+
auto peer = conductor->CreatePeerConnection(config);
29+
if (!config.is_sfu_peer) {
30+
peer_map_[peer->GetId()] = peer;
31+
}
2832
return peer;
2933
}
3034

@@ -60,7 +64,6 @@ class SignalingService {
6064
std::shared_ptr<Conductor> conductor;
6165

6266
private:
63-
bool has_candidates_in_sdp_;
6467
std::unique_ptr<Worker> worker_;
6568
std::unordered_map<std::string, rtc::scoped_refptr<RtcPeer>> peer_map_;
6669
};

src/signaling/websocket_service.cpp

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ void WebsocketService::OnHandshake(beast::error_code ec) {
125125
void WebsocketService::Read() {
126126
std::visit(
127127
[this](auto &ws) {
128+
if (!ws.is_open()) {
129+
return;
130+
}
131+
128132
ws.async_read(buffer_,
129133
[this](boost::system::error_code ec, std::size_t bytes_transferred) {
130134
if (ec) {
@@ -141,51 +145,63 @@ void WebsocketService::Read() {
141145
}
142146

143147
void WebsocketService::OnMessage(const std::string &req) {
148+
DEBUG_PRINT("Received message: %s", req.c_str());
149+
try {
150+
nlohmann::json jsonObj = nlohmann::json::parse(req.c_str());
151+
} catch (const std::exception &e) {
152+
ERROR_PRINT("Failed to parse message: %s", e.what());
153+
return;
154+
}
155+
144156
json jsonObj = json::parse(req.c_str());
145157
std::string action = jsonObj["action"];
146158
std::string message = jsonObj["message"];
147-
DEBUG_PRINT("Received message: %s", req.c_str());
148159

149160
if (action == "join") {
150161
PeerConfig config;
151-
webrtc::PeerConnectionInterface::IceServer ice_server;
162+
config.is_sfu_peer = true;
152163

164+
webrtc::PeerConnectionInterface::IceServer ice_server;
153165
nlohmann::json messageJson = nlohmann::json::parse(jsonObj["message"].get<std::string>());
154166
ice_server.urls = messageJson["urls"].get<std::vector<std::string>>();
155167
ice_server.username = messageJson["username"];
156168
ice_server.password = messageJson["credential"];
157169
config.servers.push_back(ice_server);
158170

159-
pub_peer_ = conductor->CreatePeerConnection(config);
160-
pub_peer_->OnLocalSdp(
161-
[this](const std::string &peer_id, const std::string &sdp, const std::string &type) {
171+
pub_peer_ = CreatePeer(config);
172+
if (pub_peer_) {
173+
pub_peer_->OnLocalSdp([this](const std::string &peer_id, const std::string &sdp,
174+
const std::string &type) {
162175
Write(type, sdp);
163176
});
164-
pub_peer_->OnLocalIce([this](const std::string &peer_id, const std::string &sdp_mid,
165-
int sdp_mline_index, const std::string &candidate) {
166-
Write("trickle", candidate);
167-
});
177+
pub_peer_->OnLocalIce([this](const std::string &peer_id, const std::string &sdp_mid,
178+
int sdp_mline_index, const std::string &candidate) {
179+
Write("trickle", candidate);
180+
});
181+
}
168182

169183
config.is_publisher = false;
170-
sub_peer_ = conductor->CreatePeerConnection(config);
171-
sub_peer_->OnLocalSdp(
172-
[this](const std::string &peer_id, const std::string &sdp, const std::string &type) {
184+
sub_peer_ = CreatePeer(config);
185+
if (sub_peer_) {
186+
sub_peer_->OnLocalSdp([this](const std::string &peer_id, const std::string &sdp,
187+
const std::string &type) {
173188
Write(type, sdp);
174189
});
175-
sub_peer_->OnLocalIce([this](const std::string &peer_id, const std::string &sdp_mid,
176-
int sdp_mline_index, const std::string &candidate) {
177-
Write("trickle", candidate);
178-
});
190+
sub_peer_->OnLocalIce([this](const std::string &peer_id, const std::string &sdp_mid,
191+
int sdp_mline_index, const std::string &candidate) {
192+
Write("trickle", candidate);
193+
});
194+
}
179195

180196
Write("addVideoTrack", args_.uid);
181197
if (!args_.no_audio) {
182198
Write("addAudioTrack", args_.uid);
183199
}
184-
} else if (action == "offer" && !sub_peer_->IsConnected()) {
200+
} else if (action == "offer" && sub_peer_ && !sub_peer_->IsConnected()) {
185201
sub_peer_->SetRemoteSdp(message, "offer");
186-
} else if (action == "answer" && !pub_peer_->IsConnected()) {
202+
} else if (action == "answer" && pub_peer_ && !pub_peer_->IsConnected()) {
187203
pub_peer_->SetRemoteSdp(message, "answer");
188-
} else if (action == "trackPublished") {
204+
} else if (action == "trackPublished" && pub_peer_) {
189205
pub_peer_->CreateOffer();
190206
} else if (action == "trickle") {
191207
OnRemoteIce(message);

0 commit comments

Comments
 (0)