@@ -137,17 +137,49 @@ rtc::scoped_refptr<RtcPeer> Conductor::CreatePeerConnection(PeerConfig config) {
137137
138138 peer->SetPeer (result.MoveValue ());
139139
140- if (config.is_sfu_peer ) {
141- if (!config.is_publisher ) {
142- return peer;
140+ InitializeDataChannels (peer);
141+
142+ AddTracks (peer->GetPeer ());
143+
144+ DEBUG_PRINT (" Peer connection(%s) is created! " , peer->GetId ().c_str ());
145+ return peer;
146+ }
147+
148+ void Conductor::InitializeDataChannels (rtc::scoped_refptr<RtcPeer> peer) {
149+ if (peer->isSfuPeer () && !peer->isPublisher ()) {
150+ peer->SetOnDataChannelCallback ([this ](std::shared_ptr<RtcChannel> channel) {
151+ DEBUG_PRINT (" Remote channel (%s) from sfu subscriber peer [%s]" ,
152+ channel->label ().c_str (), channel->id ().c_str ());
153+ BindDataChannelToIpcReceiver (channel);
154+ });
155+ return ;
156+ }
157+
158+ // Essential data channels(Lossy / Reliable / Command)
159+ auto lossy_channel = peer->CreateDataChannel (ChannelMode::Lossy);
160+ auto reliable_channel = peer->CreateDataChannel (ChannelMode::Reliable);
161+
162+ if (args.enable_ipc ) {
163+ switch (args.ipc_channel_mode ) {
164+ case ChannelMode::Lossy:
165+ BindIpcToDataChannel (lossy_channel);
166+ break ;
167+ case ChannelMode::Reliable:
168+ BindIpcToDataChannel (reliable_channel);
169+ break ;
170+ default :
171+ BindIpcToDataChannel (lossy_channel);
172+ BindIpcToDataChannel (reliable_channel);
173+ break ;
143174 }
144- peer->CreateDataChannel (ChannelMode::Lossy);
145- peer->CreateDataChannel (ChannelMode::Reliable);
146- } else if (args.enable_ipc ) {
147- SetupIpcDataChannel (peer, ChannelMode::Lossy);
148- SetupIpcDataChannel (peer, ChannelMode::Reliable);
149175 }
150176
177+ if (!peer->isSfuPeer ()) {
178+ InitializeCommandChannel (peer);
179+ }
180+ }
181+
182+ void Conductor::InitializeCommandChannel (rtc::scoped_refptr<RtcPeer> peer) {
151183 auto cmd_channel = peer->CreateDataChannel (ChannelMode::Command);
152184 cmd_channel->RegisterHandler (
153185 CommandType::SNAPSHOT,
@@ -169,15 +201,9 @@ rtc::scoped_refptr<RtcPeer> Conductor::CreatePeerConnection(PeerConfig config) {
169201 [this ](std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
170202 OnCameraOption (datachannel, msg);
171203 });
172-
173- AddTracks (peer->GetPeer ());
174-
175- DEBUG_PRINT (" Peer connection(%s) is created! " , peer->GetId ().c_str ());
176- return peer;
177204}
178205
179- void Conductor::OnSnapshot (std::shared_ptr<RtcChannel> datachannel,
180- const std::string &msg) {
206+ void Conductor::OnSnapshot (std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
181207 try {
182208 std::stringstream ss (msg);
183209 int num;
@@ -193,8 +219,7 @@ void Conductor::OnSnapshot(std::shared_ptr<RtcChannel> datachannel,
193219 }
194220}
195221
196- void Conductor::OnMetadata (std::shared_ptr<RtcChannel> datachannel,
197- const std::string &msg) {
222+ void Conductor::OnMetadata (std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
198223 DEBUG_PRINT (" OnMetadata msg: %s" , msg.c_str ());
199224 json jsonObj = json::parse (msg.c_str ());
200225
@@ -242,8 +267,7 @@ void Conductor::OnRecord(std::shared_ptr<RtcChannel> datachannel, const std::str
242267 }
243268}
244269
245- void Conductor::OnCameraOption (std::shared_ptr<RtcChannel> datachannel,
246- const std::string &msg) {
270+ void Conductor::OnCameraOption (std::shared_ptr<RtcChannel> datachannel, const std::string &msg) {
247271 DEBUG_PRINT (" OnCameraControl msg: %s" , msg.c_str ());
248272 json jsonObj = json::parse (msg.c_str ());
249273
@@ -325,19 +349,40 @@ void Conductor::InitializeIpcServer() {
325349 }
326350}
327351
328- void Conductor::SetupIpcDataChannel (rtc::scoped_refptr<RtcPeer> peer, ChannelMode mode) {
329- auto channel = peer->CreateDataChannel (mode);
330- if (channel && ipc_server_) {
331- ipc_server_->RegisterPeerCallback (channel->label (), [channel](const std::string &msg) {
332- channel->Send (msg);
333- });
334-
335- channel->OnClosed ([this ](const std::string &label) {
336- ipc_server_->UnregisterPeerCallback (label);
337- });
352+ void Conductor::BindIpcToDataChannel (std::shared_ptr<RtcChannel> channel) {
353+ BindIpcToDataChannelSender (channel);
354+ BindDataChannelToIpcReceiver (channel);
355+ }
338356
339- channel->RegisterHandler (CommandType::CUSTOM, [this ](const std::string &msg) {
340- ipc_server_->Write (msg);
341- });
357+ void Conductor::BindIpcToDataChannelSender (std::shared_ptr<RtcChannel> channel) {
358+ if (!channel || !ipc_server_) {
359+ ERROR_PRINT (" IPC or DataChannel is not found!" );
360+ return ;
342361 }
362+
363+ const auto id = channel->id ();
364+ const auto label = channel->label ();
365+
366+ ipc_server_->RegisterPeerCallback (id, [channel](const std::string &msg) {
367+ channel->Send (msg);
368+ });
369+ DEBUG_PRINT (" [%s] DataChannel (%s) registered to IPC server for sending." , id.c_str (),
370+ label.c_str ());
371+
372+ channel->OnClosed ([this , id, label]() {
373+ ipc_server_->UnregisterPeerCallback (id);
374+ DEBUG_PRINT (" [%s] DataChannel (%s) unregistered from IPC server." , id.c_str (),
375+ label.c_str ());
376+ });
377+ }
378+
379+ void Conductor::BindDataChannelToIpcReceiver (std::shared_ptr<RtcChannel> channel) {
380+ if (!channel || !ipc_server_)
381+ return ;
382+
383+ channel->RegisterHandler (CommandType::CUSTOM, [this ](const std::string &msg) {
384+ ipc_server_->Write (msg);
385+ });
386+ DEBUG_PRINT (" DataChannel (%s) connected to IPC server for receiving." ,
387+ channel->label ().c_str ());
343388}
0 commit comments