Skip to content

Commit

Permalink
Fix connection
Browse files Browse the repository at this point in the history
  • Loading branch information
reitowo committed May 6, 2024
1 parent 3f4a85f commit 4d177a3
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 354 deletions.
165 changes: 53 additions & 112 deletions core/peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,16 @@ bool Peer::usingTurn() {
}

void Peer::startServer() {
sdpTime = QDateTime::currentDateTimeUtc();

rtc::Configuration config;

config.mtu = 1400;
config.maxMessageSize = 512 * 1024;
config.iceServers.emplace_back("stun:stun.miwifi.com:3478");
if (!room->turnServer.isEmpty()) {
if (forceRelay) {
config.iceTransportPolicy = rtc::TransportPolicy::Relay;
}
config.iceServers.emplace_back("turn:" + room->turnServer.toStdString());
} else {
config.iceServers.emplace_back("stun:stun.miwifi.com:3478");
}

pc = std::make_unique<rtc::PeerConnection>(config);
Expand All @@ -114,47 +111,30 @@ void Peer::startServer() {

pc->onGatheringStateChange([=, this](rtc::PeerConnection::GatheringState state) {
qDebugStd("Server Gathering state: " << state);
if (state == rtc::PeerConnection::GatheringState::Complete) {
auto description = pc->localDescription();
if (description.has_value()) {
vts::server::Sdp sdp;
sdp.set_type(description.value().typeString());
sdp.set_sdp(std::string(description.value()));
sdp.set_timestamp(sdpTime.toMSecsSinceEpoch());
sdp.set_frompeerid(room->localPeerId.toStdString());
sdp.set_topeerid(remotePeerId.toStdString());
sdp.set_turn(room->turnServer.toStdString());

qDebug() << "Send server sdp to client";
room->roomServer->setSdp(sdp);
}
}
});

// pc->onLocalDescription([=, this](const rtc::Description &description) {
// vts::server::Sdp sdp;
// sdp.set_type(description.typeString());
// sdp.set_sdp(std::string(description));
// sdp.set_timestamp(sdpTime.toMSecsSinceEpoch());
// sdp.set_turn(room->turnServer.toStdString());
// sdp.set_frompeerid(room->localPeerId.toStdString());
// sdp.set_topeerid(remotePeerId.toStdString());
//
// qDebug() << "Send server sdp to client";
// room->roomServer->setSdp(sdp);
// });
//
// pc->onLocalCandidate([=, this](const rtc::Candidate &candidate) {
// vts::server::Candidate c;
// c.set_mid(candidate.mid());
// c.set_candidate(candidate.candidate());
// c.set_frompeerid(room->localPeerId.toStdString());
// c.set_topeerid(remotePeerId.toStdString());
//
// qDebug() << "Send server candidate to client";
// qDebugStd(candidate);
// room->roomServer->setCandidate(c);
// });
pc->onLocalDescription([=, this](const rtc::Description &description) {
vts::server::Sdp sdp;
sdp.set_type(description.typeString());
sdp.set_sdp(std::string(description));
sdp.set_frompeerid(room->localPeerId.toStdString());
sdp.set_topeerid(remotePeerId.toStdString());

qDebug() << "Send server sdp to client";
room->roomServer->setSdp(sdp);
});

pc->onLocalCandidate([=, this](const rtc::Candidate &candidate) {
vts::server::Candidate c;
c.set_mid(candidate.mid());
c.set_candidate(candidate.candidate());
c.set_frompeerid(room->localPeerId.toStdString());
c.set_topeerid(remotePeerId.toStdString());

qDebug() << "Send server candidate to client";
qDebugStd(candidate);
room->roomServer->setCandidate(c);
});

pcLock.lock();
dc = pc->createDataChannel("vts");
Expand All @@ -181,29 +161,18 @@ void Peer::startServer() {
});
}

void Peer::startClient(const vts::server::Sdp &serverSdp) {
auto timeStamp = serverSdp.timestamp();
auto serverSdpTime = QDateTime::fromMSecsSinceEpoch(timeStamp, Qt::UTC);

if (serverSdpTime <= sdpTime) {
qDebug() << "Client ignore old sdp";
return;
}

sdpTime = serverSdpTime;

void Peer::startClient() {
rtc::Configuration config;

config.mtu = 1400;
config.maxMessageSize = 512 * 1024;
const auto &turnServer = serverSdp.turn();
config.iceServers.emplace_back("stun:stun.miwifi.com:3478");
const auto &turnServer = room->turnServer.toStdString();
if (!turnServer.empty()) {
if (forceRelay) {
config.iceTransportPolicy = rtc::TransportPolicy::Relay;
}
config.iceServers.emplace_back("turn:" + turnServer);
} else {
config.iceServers.emplace_back("stun:stun.miwifi.com:3478");
}

pc = std::make_unique<rtc::PeerConnection>(config);
Expand All @@ -218,44 +187,29 @@ void Peer::startClient(const vts::server::Sdp &serverSdp) {

pc->onGatheringStateChange([=, this](rtc::PeerConnection::GatheringState state) {
qDebugStd("Client Gathering state: " << state);
if (state == rtc::PeerConnection::GatheringState::Complete) {
auto description = pc->localDescription();
if (description.has_value()) {
vts::server::Sdp sdp;
sdp.set_type(description.value().typeString());
sdp.set_sdp(std::string(description.value()));
sdp.set_timestamp(serverSdp.timestamp());
sdp.set_frompeerid(room->localPeerId.toStdString());
sdp.set_topeerid(remotePeerId.toStdString());

qDebug() << "Send client sdp to server";
room->roomServer->setSdp(sdp);
}
}
});

// pc->onLocalDescription([=, this](const rtc::Description &description) {
// vts::server::Sdp sdp;
// sdp.set_type(description.typeString());
// sdp.set_sdp(std::string(description));
// sdp.set_timestamp(serverSdp.timestamp());
// sdp.set_frompeerid(room->localPeerId.toStdString());
// sdp.set_topeerid(remotePeerId.toStdString());
//
// qDebug() << "Send client sdp to server";
// room->roomServer->setSdp(sdp);
// });
//
// pc->onLocalCandidate([=, this](const rtc::Candidate &candidate) {
// vts::server::Candidate c;
// c.set_mid(candidate.mid());
// c.set_candidate(candidate.candidate());
// c.set_frompeerid(room->localPeerId.toStdString());
// c.set_topeerid(remotePeerId.toStdString());
//
// qDebug() << "Send client candidate to server";
// room->roomServer->setCandidate(c);
// });
pc->onLocalDescription([=, this](const rtc::Description &description) {
vts::server::Sdp sdp;
sdp.set_type(description.typeString());
sdp.set_sdp(std::string(description));
sdp.set_frompeerid(room->localPeerId.toStdString());
sdp.set_topeerid(remotePeerId.toStdString());

qDebug() << "Send client sdp to server";
room->roomServer->setSdp(sdp);
});

pc->onLocalCandidate([=, this](const rtc::Candidate &candidate) {
vts::server::Candidate c;
c.set_mid(candidate.mid());
c.set_candidate(candidate.candidate());
c.set_frompeerid(room->localPeerId.toStdString());
c.set_topeerid(remotePeerId.toStdString());

qDebug() << "Send client candidate to server";
room->roomServer->setCandidate(c);
});

pc->onDataChannel([=, this](std::shared_ptr<rtc::DataChannel> incoming) {
pcLock.lock();
Expand Down Expand Up @@ -284,12 +238,6 @@ void Peer::startClient(const vts::server::Sdp &serverSdp) {
smartBuf.reset();
});
});

qDebug() << "set server remote sdp";
qDebugStd(pc->signalingState() << pc->state() << pc->gatheringState());
auto description = rtc::Description(serverSdp.sdp(), serverSdp.type());
pc->setRemoteDescription(description);
qDebugStd(description);
}

void Peer::close() {
Expand Down Expand Up @@ -331,32 +279,25 @@ QString Peer::dataStats() {
}

void Peer::addRemoteCandidate(const vts::server::Candidate &candidate) {
if (pc == nullptr || pc->state() == rtc::PeerConnection::State::Connected ||
pc->signalingState() == rtc::PeerConnection::SignalingState::Stable)
if (pc == nullptr || pc->state() == rtc::PeerConnection::State::Connected)
return;
if (connected())
return;

qDebug() << "set client remote candidate";
qDebug() << "add remote candidate";
auto c = rtc::Candidate(candidate.candidate(), candidate.mid());
pc->addRemoteCandidate(c);
qDebugStd(c);
}

void Peer::setClientRemoteSdp(const vts::server::Sdp &sdp) {
if (pc == nullptr || pc->state() == rtc::PeerConnection::State::Connected ||
pc->signalingState() == rtc::PeerConnection::SignalingState::Stable)
void Peer::setRemoteSdp(const vts::server::Sdp &sdp) {
if (pc == nullptr || pc->state() == rtc::PeerConnection::State::Connected)
return;
if (connected())
return;

if (sdpTime != QDateTime::fromMSecsSinceEpoch(sdp.timestamp(), Qt::UTC)) {
qDebug() << "Server ignore mismatched sdp";
return;
}

qDebug() << "set client remote sdp";
qDebugStd(pc->signalingState() << pc->state() << pc->gatheringState());
qDebug() << "set remote sdp";
qDebugStd(pc->signalingState() << " " << pc->state() << " " << pc->gatheringState());
auto description = rtc::Description(sdp.sdp(), sdp.type());
pc->setRemoteDescription(description);
qDebugStd(description);
Expand Down
5 changes: 2 additions & 3 deletions core/peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Peer {
CollabRoom* room;
std::unique_ptr<rtc::PeerConnection> pc;
std::shared_ptr<rtc::DataChannel> dc;
QDateTime sdpTime;
bool forceRelay;

std::unique_ptr<AvToDx> dec;
Expand All @@ -45,7 +44,7 @@ class Peer {
bool failed();

void startServer();
void startClient(const vts::server::Sdp& serverSdp);
void startClient();
void close();

QMutex decoderMutex;
Expand All @@ -67,7 +66,7 @@ class Peer {

QString dataStats();
void addRemoteCandidate(const vts::server::Candidate& candidate);
void setClientRemoteSdp(const vts::server::Sdp& sdp);
void setRemoteSdp(const vts::server::Sdp& sdp);
void initSmartBuf();

void decode(std::unique_ptr<vts::VtsMsg> m);
Expand Down
19 changes: 19 additions & 0 deletions core/room_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ void RoomServer::handleMessage(const vts::server::Notify &notify) {
case vts::server::Notify::kFrame:
room->onNotifyFrameFormat(notify.frame());
break;
case vts::server::Notify::kTurn:
room->onNotifyTurn(notify.turn().turn());
break;
case vts::server::Notify::kRoomDestroy: {
static QMutex destroyedMutex;
ScopedQMutex _(&destroyedMutex);
Expand Down Expand Up @@ -242,6 +245,22 @@ void RoomServer::setNick(const string &nick) {
}
}

void RoomServer::setTurn(const string &turn) {
auto context = getCtx();
if (context == nullptr)
return;

vts::server::TurnInfo t;
t.set_turn(turn);

vts::server::RspCommon rsp;
auto status = service->SetTurn(context.get(), t, &rsp);
if (!status.ok()) {
qDebug() << __FUNCTION__ << "failed:" << status.error_message().c_str();
emit room->onRoomServerError(__FUNCTION__, status.error_message().c_str());
}
}

void RoomServer::setFrameFormat(const vts::server::FrameFormatSetting &format) {
auto context = getCtx();
if (context == nullptr)
Expand Down
1 change: 1 addition & 0 deletions core/room_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class RoomServer {
void setRtt(const vts::server::ReqRtt& rtt);
void setStat(const vts::server::ReqStat& stat);
void setNick(const std::string& nick);
void setTurn(const std::string& turn);
void setSdp(const vts::server::Sdp& sdp);
void setCandidate(const vts::server::Candidate& candidate);
void setNat(int type);
Expand Down
2 changes: 1 addition & 1 deletion proto
Submodule proto updated from d2a685 to 53696e
Loading

0 comments on commit 4d177a3

Please sign in to comment.