Skip to content

Commit 8d5fc32

Browse files
committed
refactor: rename data channel class
1 parent 7c934db commit 8d5fc32

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

src/rtc/conductor.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,22 @@ rtc::scoped_refptr<RtcPeer> Conductor::CreatePeerConnection(PeerConfig config) {
151151
auto cmd_channel = peer->CreateDataChannel(ChannelMode::Command);
152152
cmd_channel->RegisterHandler(
153153
CommandType::SNAPSHOT,
154-
[this](std::shared_ptr<DataChannelSubject> datachannel, const std::string &msg) {
154+
[this](std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
155155
OnSnapshot(datachannel, msg);
156156
});
157157
cmd_channel->RegisterHandler(
158158
CommandType::METADATA,
159-
[this](std::shared_ptr<DataChannelSubject> datachannel, const std::string &msg) {
159+
[this](std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
160160
OnMetadata(datachannel, msg);
161161
});
162162
cmd_channel->RegisterHandler(
163163
CommandType::RECORDING,
164-
[this](std::shared_ptr<DataChannelSubject> datachannel, const std::string &msg) {
164+
[this](std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
165165
OnRecord(datachannel, msg);
166166
});
167167
cmd_channel->RegisterHandler(
168168
CommandType::CAMERA_OPTION,
169-
[this](std::shared_ptr<DataChannelSubject> datachannel, const std::string &msg) {
169+
[this](std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
170170
OnCameraOption(datachannel, msg);
171171
});
172172

@@ -176,7 +176,7 @@ rtc::scoped_refptr<RtcPeer> Conductor::CreatePeerConnection(PeerConfig config) {
176176
return peer;
177177
}
178178

179-
void Conductor::OnSnapshot(std::shared_ptr<DataChannelSubject> datachannel,
179+
void Conductor::OnSnapshot(std::shared_ptr<RtcChannel> datachannel,
180180
const std::string &msg) {
181181
try {
182182
std::stringstream ss(msg);
@@ -193,7 +193,7 @@ void Conductor::OnSnapshot(std::shared_ptr<DataChannelSubject> datachannel,
193193
}
194194
}
195195

196-
void Conductor::OnMetadata(std::shared_ptr<DataChannelSubject> datachannel,
196+
void Conductor::OnMetadata(std::shared_ptr<RtcChannel> datachannel,
197197
const std::string &msg) {
198198
DEBUG_PRINT("OnMetadata msg: %s", msg.c_str());
199199
json jsonObj = json::parse(msg.c_str());
@@ -225,7 +225,7 @@ void Conductor::OnMetadata(std::shared_ptr<DataChannelSubject> datachannel,
225225
}
226226
}
227227

228-
void Conductor::OnRecord(std::shared_ptr<DataChannelSubject> datachannel, const std::string &path) {
228+
void Conductor::OnRecord(std::shared_ptr<RtcChannel> datachannel, const std::string &path) {
229229
if (args.record_path.empty()) {
230230
return;
231231
}
@@ -242,7 +242,7 @@ void Conductor::OnRecord(std::shared_ptr<DataChannelSubject> datachannel, const
242242
}
243243
}
244244

245-
void Conductor::OnCameraOption(std::shared_ptr<DataChannelSubject> datachannel,
245+
void Conductor::OnCameraOption(std::shared_ptr<RtcChannel> datachannel,
246246
const std::string &msg) {
247247
DEBUG_PRINT("OnCameraControl msg: %s", msg.c_str());
248248
json jsonObj = json::parse(msg.c_str());

src/rtc/conductor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class Conductor {
3737

3838
void SetupIpcDataChannel(rtc::scoped_refptr<RtcPeer> peer, ChannelMode mode);
3939
void AddTracks(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection);
40-
void OnSnapshot(std::shared_ptr<DataChannelSubject> datachannel, const std::string &msg);
41-
void OnMetadata(std::shared_ptr<DataChannelSubject> datachannel, const std::string &path);
42-
void OnRecord(std::shared_ptr<DataChannelSubject> datachannel, const std::string &path);
43-
void OnCameraOption(std::shared_ptr<DataChannelSubject> datachannel, const std::string &msg);
40+
void OnSnapshot(std::shared_ptr<RtcChannel> datachannel, const std::string &msg);
41+
void OnMetadata(std::shared_ptr<RtcChannel> datachannel, const std::string &path);
42+
void OnRecord(std::shared_ptr<RtcChannel> datachannel, const std::string &path);
43+
void OnCameraOption(std::shared_ptr<RtcChannel> datachannel, const std::string &msg);
4444

4545
std::unique_ptr<rtc::Thread> network_thread_;
4646
std::unique_ptr<rtc::Thread> worker_thread_;

src/rtc/data_channel_subject.cpp renamed to src/rtc/rtc_channel.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
#include "rtc/data_channel_subject.h"
1+
#include "rtc/rtc_channel.h"
22

33
#include "common/logging.h"
44

55
const int CHUNK_SIZE = 65536;
66

7-
std::shared_ptr<DataChannelSubject>
8-
DataChannelSubject::Create(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
9-
return std::make_shared<DataChannelSubject>(std::move(data_channel));
7+
std::shared_ptr<RtcChannel>
8+
RtcChannel::Create(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
9+
return std::make_shared<RtcChannel>(std::move(data_channel));
1010
}
1111

12-
DataChannelSubject::DataChannelSubject(
12+
RtcChannel::RtcChannel(
1313
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
1414
data_channel_ = std::move(data_channel);
1515
label_ = data_channel_->label();
1616
data_channel_->RegisterObserver(this);
1717
}
1818

19-
DataChannelSubject::~DataChannelSubject() {
19+
RtcChannel::~RtcChannel() {
2020
DEBUG_PRINT("datachannel (%s) is released!", label_.c_str());
2121
}
2222

23-
std::string DataChannelSubject::label() const { return label_; }
23+
std::string RtcChannel::label() const { return label_; }
2424

25-
void DataChannelSubject::OnStateChange() {
25+
void RtcChannel::OnStateChange() {
2626
webrtc::DataChannelInterface::DataState state = data_channel_->state();
2727
DEBUG_PRINT("[%s] OnStateChange => %s", data_channel_->label().c_str(),
2828
webrtc::DataChannelInterface::DataStateString(state));
2929
}
3030

31-
void DataChannelSubject::Terminate() {
31+
void RtcChannel::Terminate() {
3232
UnSubscribe();
3333
data_channel_->UnregisterObserver();
3434
data_channel_->Close();
@@ -37,19 +37,19 @@ void DataChannelSubject::Terminate() {
3737
}
3838
}
3939

40-
void DataChannelSubject::OnClosed(std::function<void(const std::string &)> func) {
40+
void RtcChannel::OnClosed(std::function<void(const std::string &)> func) {
4141
on_closed_func_ = std::move(func);
4242
}
4343

44-
void DataChannelSubject::OnMessage(const webrtc::DataBuffer &buffer) {
44+
void RtcChannel::OnMessage(const webrtc::DataBuffer &buffer) {
4545
const uint8_t *data = buffer.data.data<uint8_t>();
4646
size_t length = buffer.data.size();
4747
std::string message(reinterpret_cast<const char *>(data), length);
4848

4949
Next(message);
5050
}
5151

52-
void DataChannelSubject::RegisterHandler(CommandType type, ChannelCommandHandler func) {
52+
void RtcChannel::RegisterHandler(CommandType type, ChannelCommandHandler func) {
5353
auto observer = AsObservable(type);
5454
observer->Subscribe([self = shared_from_this(), func](std::string message) {
5555
if (!message.empty()) {
@@ -58,7 +58,7 @@ void DataChannelSubject::RegisterHandler(CommandType type, ChannelCommandHandler
5858
});
5959
}
6060

61-
void DataChannelSubject::RegisterHandler(CommandType type, PayloadHandler func) {
61+
void RtcChannel::RegisterHandler(CommandType type, PayloadHandler func) {
6262
auto observer = AsObservable(type);
6363
observer->Subscribe([func](std::string message) {
6464
if (!message.empty()) {
@@ -67,7 +67,7 @@ void DataChannelSubject::RegisterHandler(CommandType type, PayloadHandler func)
6767
});
6868
}
6969

70-
void DataChannelSubject::Next(std::string message) {
70+
void RtcChannel::Next(std::string message) {
7171
try {
7272
json jsonObj = json::parse(message.c_str());
7373

@@ -92,26 +92,26 @@ void DataChannelSubject::Next(std::string message) {
9292
}
9393
}
9494

95-
std::shared_ptr<Observable<std::string>> DataChannelSubject::AsObservable() {
95+
std::shared_ptr<Observable<std::string>> RtcChannel::AsObservable() {
9696
auto observer = std::make_shared<Observable<std::string>>();
9797
observers_map_[CommandType::CUSTOM].push_back(observer);
9898
return observer;
9999
}
100100

101-
std::shared_ptr<Observable<std::string>> DataChannelSubject::AsObservable(CommandType type) {
101+
std::shared_ptr<Observable<std::string>> RtcChannel::AsObservable(CommandType type) {
102102
auto observer = std::make_shared<Observable<std::string>>();
103103
observers_map_[type].push_back(observer);
104104
return observer;
105105
}
106106

107-
void DataChannelSubject::UnSubscribe() {
107+
void RtcChannel::UnSubscribe() {
108108
for (auto &[type, observers] : observers_map_) {
109109
observers.clear();
110110
}
111111
observers_map_.clear();
112112
}
113113

114-
void DataChannelSubject::Send(CommandType type, const uint8_t *data, size_t size) {
114+
void RtcChannel::Send(CommandType type, const uint8_t *data, size_t size) {
115115
int bytes_read = 0;
116116
const size_t header_size = sizeof(CommandType);
117117

@@ -139,7 +139,7 @@ void DataChannelSubject::Send(CommandType type, const uint8_t *data, size_t size
139139
}
140140
}
141141

142-
void DataChannelSubject::Send(const uint8_t *data, size_t size) {
142+
void RtcChannel::Send(const uint8_t *data, size_t size) {
143143
if (data_channel_->state() != webrtc::DataChannelInterface::kOpen) {
144144
return;
145145
}
@@ -148,7 +148,7 @@ void DataChannelSubject::Send(const uint8_t *data, size_t size) {
148148
data_channel_->Send(data_buffer);
149149
}
150150

151-
void DataChannelSubject::Send(MetaMessage metadata) {
151+
void RtcChannel::Send(MetaMessage metadata) {
152152
if (metadata.path.empty()) {
153153
return;
154154
}
@@ -164,7 +164,7 @@ void DataChannelSubject::Send(MetaMessage metadata) {
164164
Send(type, nullptr, 0);
165165
}
166166

167-
void DataChannelSubject::Send(Buffer image) {
167+
void RtcChannel::Send(Buffer image) {
168168
auto type = CommandType::SNAPSHOT;
169169
const int file_size = image.length;
170170
std::string size_str = std::to_string(file_size);
@@ -175,7 +175,7 @@ void DataChannelSubject::Send(Buffer image) {
175175
DEBUG_PRINT("Image sent: %d bytes", file_size);
176176
}
177177

178-
void DataChannelSubject::Send(std::ifstream &file) {
178+
void RtcChannel::Send(std::ifstream &file) {
179179
std::vector<char> buffer(CHUNK_SIZE);
180180
int bytes_read = 0;
181181
int count = 0;
@@ -205,7 +205,7 @@ void DataChannelSubject::Send(std::ifstream &file) {
205205
Send(type, nullptr, 0);
206206
}
207207

208-
void DataChannelSubject::Send(const std::string &message) {
208+
void RtcChannel::Send(const std::string &message) {
209209
auto type = CommandType::CUSTOM;
210210
auto body = message;
211211
int body_size = message.length();

src/rtc/data_channel_subject.h renamed to src/rtc/rtc_channel.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ struct MetaMessage {
7070
}
7171
};
7272

73-
class DataChannelSubject : public webrtc::DataChannelObserver,
73+
class RtcChannel : public webrtc::DataChannelObserver,
7474
public Subject<std::string>,
75-
public std::enable_shared_from_this<DataChannelSubject> {
75+
public std::enable_shared_from_this<RtcChannel> {
7676
public:
7777
using ChannelCommandHandler =
78-
std::function<void(std::shared_ptr<DataChannelSubject>, const std::string &)>;
78+
std::function<void(std::shared_ptr<RtcChannel>, const std::string &)>;
7979
using PayloadHandler = std::function<void(const std::string &)>;
8080

81-
static std::shared_ptr<DataChannelSubject>
81+
static std::shared_ptr<RtcChannel>
8282
Create(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);
8383

84-
DataChannelSubject(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);
85-
~DataChannelSubject();
84+
RtcChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);
85+
~RtcChannel();
8686

8787
std::string label() const;
8888

src/rtc/rtc_peer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void RtcPeer::SetPeer(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer)
7070

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

73-
std::shared_ptr<DataChannelSubject> RtcPeer::CreateDataChannel(ChannelMode mode) {
73+
std::shared_ptr<RtcChannel> RtcPeer::CreateDataChannel(ChannelMode mode) {
7474
struct webrtc::DataChannelInit init;
7575
init.ordered = true;
7676
init.id = static_cast<int>(mode);
@@ -89,7 +89,7 @@ std::shared_ptr<DataChannelSubject> RtcPeer::CreateDataChannel(ChannelMode mode)
8989
return nullptr;
9090
}
9191

92-
auto channel = DataChannelSubject::Create(result.MoveValue());
92+
auto channel = RtcChannel::Create(result.MoveValue());
9393

9494
if (mode == ChannelMode::Command) {
9595
DEBUG_PRINT("The Command data channel is established successfully.");

src/rtc/rtc_peer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "args.h"
1212
#include "common/logging.h"
13-
#include "rtc/data_channel_subject.h"
13+
#include "rtc/rtc_channel.h"
1414

1515
enum ChannelMode {
1616
Command,
@@ -108,7 +108,7 @@ class RtcPeer : public webrtc::PeerConnectionObserver,
108108
void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame> *video_sink_obj);
109109
void SetPeer(rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer);
110110
rtc::scoped_refptr<webrtc::PeerConnectionInterface> GetPeer();
111-
std::shared_ptr<DataChannelSubject> CreateDataChannel(ChannelMode mode);
111+
std::shared_ptr<RtcChannel> CreateDataChannel(ChannelMode mode);
112112
std::string RestartIce(std::string ice_ufrag, std::string ice_pwd);
113113

114114
// SignalingMessageObserver implementation.
@@ -146,9 +146,9 @@ class RtcPeer : public webrtc::PeerConnectionObserver,
146146
webrtc::PeerConnectionInterface::SignalingState signaling_state_;
147147
std::unique_ptr<webrtc::SessionDescriptionInterface> modified_desc_;
148148

149-
std::shared_ptr<DataChannelSubject> cmd_channel_;
150-
std::shared_ptr<DataChannelSubject> lossy_channel_;
151-
std::shared_ptr<DataChannelSubject> reliable_channel_;
149+
std::shared_ptr<RtcChannel> cmd_channel_;
150+
std::shared_ptr<RtcChannel> lossy_channel_;
151+
std::shared_ptr<RtcChannel> reliable_channel_;
152152
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
153153
rtc::VideoSinkInterface<webrtc::VideoFrame> *custom_video_sink_;
154154
};

0 commit comments

Comments
 (0)