23
23
using namespace rtc ;
24
24
namespace owt {
25
25
namespace p2p {
26
- enum IcsP2PError : int {
27
- kWebrtcIceGatheringPolicyUnsupported = 2601 ,
28
- };
26
+
27
+ const std::string kMessageTypeKey = " type" ;
28
+ const std::string kMessageDataKey = " data" ;
29
+ const std::string kSessionDescriptionTypeKey = " type" ;
30
+ const std::string kChatClosed = " chat-closed" ;
31
+ const std::string kChatSignal = " chat-signal" ;
32
+ const std::string kSdpTypeOffer = " offer" ;
29
33
30
34
P2PClient::P2PClient (
31
35
P2PClientConfiguration& configuration,
@@ -239,7 +243,7 @@ void P2PClient::SetLocalId(const std::string& local_id) {
239
243
}
240
244
void P2PClient::OnSignalingMessage (const std::string& message,
241
245
const std::string& remote_id) {
242
- RTC_LOG (LS_WARNING ) << " Receiving signaling message from remote:" << message;
246
+ RTC_LOG (LS_VERBOSE ) << " Receiving signaling message from remote:" << message;
243
247
std::weak_ptr<P2PClient> weak_this = shared_from_this ();
244
248
signaling_queue_->PostTask ([weak_this, remote_id, message]() {
245
249
auto that = weak_this.lock ();
@@ -251,79 +255,52 @@ void P2PClient::OnSignalingMessage(const std::string& message,
251
255
<< " Chat cannot be setup since the remote user is not allowed." ;
252
256
return ;
253
257
}
258
+ Json::Reader reader;
259
+ Json::Value json_message;
260
+ if (!reader.parse (message, json_message)) {
261
+ RTC_LOG (LS_WARNING) << " Cannot parse incoming message." ;
262
+ return ;
263
+ }
264
+ std::string message_type;
265
+ rtc::GetStringFromJsonObject (json_message, kMessageTypeKey , &message_type);
254
266
if (!that->IsPeerConnectionChannelCreated (remote_id)) {
255
- if (message. find ( " \" type \" : \" chat-closed \" " ) != std::string::npos ) {
267
+ if (message_type == kChatClosed ) {
256
268
RTC_LOG (LS_WARNING) << " Non-existed chat cannot be stopped." ;
257
269
return ;
258
270
}
259
- } else if (message.find (" \" type\" :\" offer\" " ) != std::string::npos) {
260
- RTC_LOG (LS_ERROR) << " Received offer from remote." ;
261
- // If we don't have a PC before we receive an offer.
262
- auto pcc = that->GetPeerConnectionChannel (remote_id);
263
- if (pcc->HaveLocalOffer () && that->local_id_ .compare (remote_id) > 0 ) {
264
- // If our ID is larger than remote, make the remote side as the
265
- // publisher (offerer) In case we already have an offer. So we
266
- // remove current PCC and create answer.
267
- std::shared_ptr<LocalStream> stream = pcc->GetLatestLocalStream ();
268
- std::function<void ()> success_callback =
269
- pcc->GetLatestPublishSuccessCallback ();
270
- std::function<void (std::unique_ptr<Exception>)> failure_callback =
271
- pcc->GetLatestPublishFailureCallback ();
272
- pcc->Stop (nullptr , nullptr );
273
- {
274
- // If we already created offer,
275
- const std::lock_guard<std::mutex> lock (that->pc_channels_mutex_ );
276
- that->pc_channels_ .erase (remote_id);
277
- }
278
- auto new_pcc = that->GetPeerConnectionChannel (remote_id);
279
- new_pcc->OnIncomingSignalingMessage (message);
280
- new_pcc->Publish (stream, success_callback, failure_callback);
281
- return ;
282
- }
283
- } else if (message.find (" \" type\" :\" chat-closed\" " ) != std::string::npos) {
284
- RTC_LOG (LS_ERROR) << " Handle the situation that PCC is created by "
285
- " received Chat-closed." ;
286
- int code = 0 ;
287
- std::string error = " " ;
288
- Json::Reader reader;
289
- Json::Value json_message;
290
- if (reader.parse (message, json_message)) {
291
- Json::Value stop_info;
292
- rtc::GetValueFromJsonObject (json_message, " data" , &stop_info);
293
- rtc::GetIntFromJsonObject (stop_info, " code" , &code);
294
- rtc::GetStringFromJsonObject (stop_info, " message" , &error);
295
-
296
- if (code == kWebrtcIceGatheringPolicyUnsupported ) {
297
- auto pcc = that->GetPeerConnectionChannel (remote_id);
271
+ } else if (message_type == kChatSignal ){
272
+ Json::Value signal;
273
+ rtc::GetValueFromJsonObject (json_message, kMessageDataKey , &signal);
274
+ std::string sdp_type;
275
+ rtc::GetStringFromJsonObject (signal, kSessionDescriptionTypeKey , &sdp_type);
276
+ if (sdp_type == kSdpTypeOffer ) {
277
+ // If we don't have a PC before we receive an offer.
278
+ auto pcc = that->GetPeerConnectionChannel (remote_id);
279
+ if (pcc->HaveLocalOffer () && that->local_id_ .compare (remote_id) > 0 ) {
280
+ // If our ID is larger than remote, make the remote side as the
281
+ // publisher (offerer) In case we already have an offer. So we
282
+ // remove current PCC and create answer.
298
283
std::shared_ptr<LocalStream> stream = pcc->GetLatestLocalStream ();
299
284
std::function<void ()> success_callback =
300
285
pcc->GetLatestPublishSuccessCallback ();
301
286
std::function<void (std::unique_ptr<Exception>)> failure_callback =
302
287
pcc->GetLatestPublishFailureCallback ();
303
- pcc->SetAbandoned ( );
288
+ pcc->Stop ( nullptr , nullptr );
304
289
{
290
+ // If we already created offer,
305
291
const std::lock_guard<std::mutex> lock (that->pc_channels_mutex_ );
306
292
that->pc_channels_ .erase (remote_id);
307
293
}
308
-
309
294
auto new_pcc = that->GetPeerConnectionChannel (remote_id);
295
+ new_pcc->OnIncomingSignalingMessage (json_message);
310
296
new_pcc->Publish (stream, success_callback, failure_callback);
311
297
return ;
312
298
}
313
- auto pcc = that->GetPeerConnectionChannel (remote_id);
314
- // Don't send stop to remote.
315
- pcc->SetAbandoned ();
316
- {
317
- const std::lock_guard<std::mutex> lock (that->pc_channels_mutex_ );
318
- that->pc_channels_ .erase (remote_id);
319
- }
320
-
321
- return ;
322
299
}
323
300
}
324
301
// Secondly dispatch the message to pcc.
325
302
auto pcc = that->GetPeerConnectionChannel (remote_id);
326
- pcc->OnIncomingSignalingMessage (message );
303
+ pcc->OnIncomingSignalingMessage (json_message );
327
304
});
328
305
}
329
306
void P2PClient::OnServerDisconnected () {
0 commit comments