Skip to content

Commit

Permalink
Use new negotiate
Browse files Browse the repository at this point in the history
  • Loading branch information
reitowo committed May 6, 2024
1 parent 2f94991 commit 3f4a85f
Show file tree
Hide file tree
Showing 9 changed files with 1,989 additions and 2,204 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

96 changes: 75 additions & 21 deletions core/peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ void Peer::startServer() {

config.mtu = 1400;
config.maxMessageSize = 512 * 1024;
config.iceServers.emplace_back("stun:stun.qq.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 @@ -111,16 +112,14 @@ void Peer::startServer() {
}
});

pc->onGatheringStateChange([=](rtc::PeerConnection::GatheringState state) {
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()) {
auto desc = processLocalDescription(description.value());

vts::server::Sdp sdp;
sdp.set_type(desc.typeString());
sdp.set_sdp(std::string(desc));
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());
Expand All @@ -132,6 +131,31 @@ void Peer::startServer() {
}
});

// 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);
// });

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

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

Expand All @@ -172,13 +196,14 @@ void Peer::startClient(const vts::server::Sdp& serverSdp) {

config.mtu = 1400;
config.maxMessageSize = 512 * 1024;
config.iceServers.emplace_back("stun:stun.qq.com:3478");
auto turnServer = serverSdp.turn();
const auto &turnServer = serverSdp.turn();
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 @@ -196,11 +221,9 @@ void Peer::startClient(const vts::server::Sdp& serverSdp) {
if (state == rtc::PeerConnection::GatheringState::Complete) {
auto description = pc->localDescription();
if (description.has_value()) {
auto desc = processLocalDescription(description.value());

vts::server::Sdp sdp;
sdp.set_type(desc.typeString());
sdp.set_sdp(std::string(desc));
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());
Expand All @@ -211,6 +234,29 @@ void Peer::startClient(const vts::server::Sdp& serverSdp) {
}
});

// 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->onDataChannel([=, this](std::shared_ptr<rtc::DataChannel> incoming) {
pcLock.lock();
dc = std::move(incoming);
Expand Down Expand Up @@ -284,7 +330,20 @@ QString Peer::dataStats() {
.arg(humanizeBytes(pc->bytesReceived()));
}

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

qDebug() << "set client 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)
return;
Expand Down Expand Up @@ -392,21 +451,16 @@ size_t Peer::rxBytes() {
return pc->bytesReceived();
}

rtc::Description Peer::processLocalDescription(rtc::Description desc) {
auto candidates = desc.extractCandidates();
auto ret = desc;
ret.addCandidates(candidates);
return ret;
}

static QMutex rxSpeedMutex;

size_t Peer::rxSpeed() {
ScopedQMutex _(&rxSpeedMutex);
rxSpeedCount.update(rxBytes());
return rxSpeedCount.speedBytes();
}

static QMutex txSpeedMutex;

size_t Peer::txSpeed() {
ScopedQMutex _(&txSpeedMutex);
txSpeedCount.update(txBytes());
Expand Down
3 changes: 1 addition & 2 deletions core/peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ class Peer {
std::unique_ptr<smart_buf> smartBuf;
void sendAsync(std::shared_ptr<vts::VtsMsg> payload);

static rtc::Description processLocalDescription(rtc::Description desc);

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

Expand Down
18 changes: 17 additions & 1 deletion core/room_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ void RoomServer::handleMessage(const vts::server::Notify &notify) {
case vts::server::Notify::kSdp:
room->onNotifySdp(notify.sdp());
break;
case vts::server::Notify::kCandidate:
room->onNotifyCandidate(notify.candidate());
break;
case vts::server::Notify::kFrame:
room->onNotifyFrameFormat(notify.frame());
break;
Expand Down Expand Up @@ -75,7 +78,7 @@ void RoomServer::startNatTypeDetect() {
natThread = std::unique_ptr<QThread>(QThread::create([=, this]() {
qDebug() << "Start nat type determine";
CNatProb natProb;
if (!natProb.Init("stun.qq.com")) {
if (!natProb.Init("stun.miwifi.com")) {
qDebug() << "natProb init failed.";
}
int retry = 0;
Expand Down Expand Up @@ -168,6 +171,19 @@ void RoomServer::setSdp(const vts::server::Sdp &sdp) {
}
}

void RoomServer::setCandidate(const vts::server::Candidate& candidate) {
auto context = getCtx();
if (context == nullptr)
return;

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

void RoomServer::setNat(int type) {
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 @@ -55,6 +55,7 @@ class RoomServer {
void setStat(const vts::server::ReqStat& stat);
void setNick(const std::string& nick);
void setSdp(const vts::server::Sdp& sdp);
void setCandidate(const vts::server::Candidate& candidate);
void setNat(int type);
void setFrameFormat(const vts::server::FrameFormatSetting& format);
void setShareInfo(const std::string& gpu, const std::string& capture, bool start);
Expand Down
Loading

0 comments on commit 3f4a85f

Please sign in to comment.