Skip to content

Commit bf8a353

Browse files
committed
fix: auto disconnect if lossy and reliable datachannel is not exist
1 parent b630236 commit bf8a353

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/args.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ struct Args {
4747
std::string ws_host = "192.168.4.21";
4848
std::string ws_token =
4949
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
50-
"eyJleHAiOjE3NzM4OTI2NjEsImlzcyI6IkFQSVduUVRzNHRtVVp2QSIsIm5iZiI6MTc0MjM1NjY2MSwic3ViIj"
51-
"oiMTMxZGZjMzItNjRlMi00YjZiLTllZGEtZDdjYTU5NTNjYWJlIiwidmlkZW8iOnsicm9vbSI6ImRldmljZS0x"
52-
"Iiwicm9vbUpvaW4iOnRydWV9fQ.ThQTHYd8CBR0t3epwcak6oaleeu760V96UF8GbOMUks";
50+
"eyJleHAiOjE3NzQ3NDU2MzksImlzcyI6IkFQSVduUVRzNHRtVVp2QSIsIm5iZiI6MTc0MzIwOTYzOSwic3ViIjoiZj"
51+
"JkNzRiOTUtMmYxNi00ODRiLTg3NjctYThjNWY3NzFlZWY2IiwidmlkZW8iOnsiY2FuUHVibGlzaCI6dHJ1ZSwiY2Fu"
52+
"UHVibGlzaERhdGEiOnRydWUsImNhblN1YnNjcmliZSI6ZmFsc2UsInJvb20iOiJkZXZpY2UtMSIsInJvb21Kb2luIj"
53+
"p0cnVlfX0.o7e-gjkqfMpeDjATwaWWLKUWPa8RlaoIOcuw3p8FxQk";
5354
};
5455

5556
#endif // ARGS_H_

src/conductor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ rtc::scoped_refptr<RtcPeer> Conductor::CreatePeerConnection(PeerConfig config) {
125125

126126
peer->SetPeer(result.MoveValue());
127127

128+
if (config.is_sfu_peer) {
129+
peer->CreateDataChannel(ChannelLabel::Lossy);
130+
peer->CreateDataChannel(ChannelLabel::Reliable);
131+
}
132+
128133
if (!config.is_publisher) {
129134
return peer;
130135
}

src/rtc_peer.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,35 @@ void RtcPeer::SetPeer(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer)
6161

6262
rtc::scoped_refptr<webrtc::PeerConnectionInterface> RtcPeer::GetPeer() { return peer_connection_; }
6363

64-
void RtcPeer::CreateDataChannel() {
64+
void RtcPeer::CreateDataChannel(ChannelLabel label) {
6565
struct webrtc::DataChannelInit init;
6666
init.ordered = true;
6767
init.reliable = true;
68-
init.id = 0;
69-
auto result = peer_connection_->CreateDataChannelOrError("cmd_channel", &init);
68+
init.id = static_cast<int>(label);
69+
70+
if (label == ChannelLabel::Lossy) {
71+
init.maxRetransmits = 0;
72+
}
73+
74+
auto result = peer_connection_->CreateDataChannelOrError(RtcPeer::LabelToString(label), &init);
7075

7176
if (!result.ok()) {
7277
ERROR_PRINT("Failed to create data channel.");
7378
return;
7479
}
7580

76-
DEBUG_PRINT("The data channel is established successfully.");
77-
data_channel_subject_ = std::make_shared<DataChannelSubject>();
78-
data_channel_subject_->SetDataChannel(result.MoveValue());
81+
if (label == ChannelLabel::Command) {
82+
DEBUG_PRINT("The data channel is established successfully.");
83+
data_channel_subject_ = std::make_shared<DataChannelSubject>();
84+
data_channel_subject_->SetDataChannel(result.MoveValue());
7985

80-
auto conn_observer = data_channel_subject_->AsObservable(CommandType::CONNECT);
81-
conn_observer->Subscribe([this](std::string message) {
82-
if (message == "false") { // todo: use enum or so.
83-
peer_connection_->Close();
84-
}
85-
});
86+
auto conn_observer = data_channel_subject_->AsObservable(CommandType::CONNECT);
87+
conn_observer->Subscribe([this](std::string message) {
88+
if (message == "false") { // todo: use enum or so.
89+
peer_connection_->Close();
90+
}
91+
});
92+
}
8693
}
8794

8895
std::string RtcPeer::RestartIce(std::string ice_ufrag, std::string ice_pwd) {

src/rtc_peer.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
#include "common/logging.h"
1313
#include "data_channel_subject.h"
1414

15+
enum ChannelLabel {
16+
Command,
17+
Lossy,
18+
Reliable
19+
};
20+
1521
struct PeerConfig : public webrtc::PeerConnectionInterface::RTCConfiguration {
1622
int timeout = 10;
1723
bool is_publisher = true;
@@ -91,7 +97,7 @@ class RtcPeer : public webrtc::PeerConnectionObserver,
9197
void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame> *video_sink_obj);
9298
void SetPeer(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer);
9399
rtc::scoped_refptr<webrtc::PeerConnectionInterface> GetPeer();
94-
void CreateDataChannel();
100+
void CreateDataChannel(ChannelLabel label = ChannelLabel::Command);
95101
std::string RestartIce(std::string ice_ufrag, std::string ice_pwd);
96102
void OnSnapshot(OnCommand func);
97103
void OnMetadata(OnCommand func);
@@ -103,6 +109,19 @@ class RtcPeer : public webrtc::PeerConnectionObserver,
103109
void SetRemoteIce(const std::string &sdp_mid, int sdp_mline_index,
104110
const std::string &candidate) override;
105111

112+
static std::string LabelToString(ChannelLabel label) {
113+
switch (label) {
114+
case Command:
115+
return "cmd_channel";
116+
case Lossy:
117+
return "_lossy";
118+
case Reliable:
119+
return "_reliable";
120+
default:
121+
return "unknown";
122+
}
123+
}
124+
106125
private:
107126
void SubscribeCommandChannel(CommandType type, OnCommand func);
108127

0 commit comments

Comments
 (0)