diff --git a/src/PeerConnectionManager.cpp b/src/PeerConnectionManager.cpp index ee87d20e..36f42b20 100755 --- a/src/PeerConnectionManager.cpp +++ b/src/PeerConnectionManager.cpp @@ -658,47 +658,47 @@ const Json::Value PeerConnectionManager::createOffer(const std::string &peerid, if (!this->AddStreams(peerConnection.get(), videourl, audiourl, options)) { - RTC_LOG(LS_WARNING) << "Can't add stream"; - } - - // register peerid - { - std::lock_guard peerlock(m_peerMapMutex); - m_peer_connectionobs_map.insert(std::pair(peerid, peerConnectionObserver)); - } + RTC_LOG(LS_ERROR) << "Can't add stream"; + } else { + // register peerid + { + std::lock_guard peerlock(m_peerMapMutex); + m_peer_connectionobs_map.insert(std::pair(peerid, peerConnectionObserver)); + } - // ask to create offer - webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtcoptions; - rtcoptions.offer_to_receive_video = 0; - rtcoptions.offer_to_receive_audio = 0; - std::promise localpromise; - rtc::scoped_refptr localSessionObserver(CreateSessionDescriptionObserver::Create(peerConnection, localpromise)); - peerConnection->CreateOffer(localSessionObserver.get(), rtcoptions); - - // waiting for offer - std::future future = localpromise.get_future(); - if (future.wait_for(std::chrono::milliseconds(5000)) == std::future_status::ready) - { - // answer with the created offer - const webrtc::SessionDescriptionInterface *desc = future.get(); - if (desc) + // ask to create offer + webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtcoptions; + rtcoptions.offer_to_receive_video = 0; + rtcoptions.offer_to_receive_audio = 0; + std::promise localpromise; + rtc::scoped_refptr localSessionObserver(CreateSessionDescriptionObserver::Create(peerConnection, localpromise)); + peerConnection->CreateOffer(localSessionObserver.get(), rtcoptions); + + // waiting for offer + std::future future = localpromise.get_future(); + if (future.wait_for(std::chrono::milliseconds(5000)) == std::future_status::ready) { - std::string sdp; - desc->ToString(&sdp); + // answer with the created offer + const webrtc::SessionDescriptionInterface *desc = future.get(); + if (desc) + { + std::string sdp; + desc->ToString(&sdp); - offer[kSessionDescriptionTypeName] = desc->type(); - offer[kSessionDescriptionSdpName] = sdp; + offer[kSessionDescriptionTypeName] = desc->type(); + offer[kSessionDescriptionSdpName] = sdp; + } + else + { + RTC_LOG(LS_ERROR) << "Failed to create offer - no session"; + } } else { - RTC_LOG(LS_ERROR) << "Failed to create offer - no session"; + localSessionObserver->cancel(); + RTC_LOG(LS_ERROR) << "Failed to create offer - timeout"; } } - else - { - localSessionObserver->cancel(); - RTC_LOG(LS_ERROR) << "Failed to create offer - timeout"; - } } return offer; } @@ -805,60 +805,62 @@ std::unique_ptr PeerConnectionManager::getA // add local stream if (!this->AddStreams(peerConnection.get(), videourl, audiourl, options)) { - RTC_LOG(LS_WARNING) << "Can't add stream"; - } - - // set remote offer - std::promise remotepromise; - rtc::scoped_refptr remoteSessionObserver(SetSessionDescriptionObserver::Create(peerConnection, remotepromise)); - peerConnection->SetRemoteDescription(remoteSessionObserver.get(), session_description); - // waiting for remote description - std::future remotefuture = remotepromise.get_future(); - if (remotefuture.wait_for(std::chrono::milliseconds(5000)) == std::future_status::ready) - { - RTC_LOG(LS_INFO) << "remote_description is ready"; - } - else - { - remoteSessionObserver->cancel(); - RTC_LOG(LS_WARNING) << "remote_description is NULL"; - } + RTC_LOG(LS_ERROR) << "Can't add stream"; + } else { + // set remote offer + std::promise remotepromise; + rtc::scoped_refptr remoteSessionObserver(SetSessionDescriptionObserver::Create(peerConnection, remotepromise)); + peerConnection->SetRemoteDescription(remoteSessionObserver.get(), session_description); + // waiting for remote description + std::future remotefuture = remotepromise.get_future(); + if (remotefuture.wait_for(std::chrono::milliseconds(5000)) == std::future_status::ready) + { + RTC_LOG(LS_INFO) << "remote_description is ready"; + } + else + { + remoteSessionObserver->cancel(); + RTC_LOG(LS_ERROR) << "remote_description timeout"; + } - // create answer - webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtcoptions; - std::promise localpromise; - rtc::scoped_refptr localSessionObserver(CreateSessionDescriptionObserver::Create(peerConnection, localpromise)); - peerConnection->CreateAnswer(localSessionObserver.get(), rtcoptions); - - // wait gathering completion - if (waitgatheringcompletion) { - int retry = 5; - while ( (peerConnectionObserver->getGatheringState() != webrtc::PeerConnectionInterface::IceGatheringState::kIceGatheringComplete) && (retry > 0) ) { - RTC_LOG(LS_ERROR) << "waiting..." << retry; - retry --; - std::this_thread::sleep_for(std::chrono::milliseconds(250)); + // create answer + webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtcoptions; + std::promise localpromise; + rtc::scoped_refptr localSessionObserver(CreateSessionDescriptionObserver::Create(peerConnection, localpromise)); + peerConnection->CreateAnswer(localSessionObserver.get(), rtcoptions); + + // wait gathering completion + if (waitgatheringcompletion) { + int retry = 5; + while ( (peerConnectionObserver->getGatheringState() != webrtc::PeerConnectionInterface::IceGatheringState::kIceGatheringComplete) && (retry > 0) ) { + RTC_LOG(LS_ERROR) << "waiting..." << retry; + retry --; + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + } } - } - // waiting for answer - std::future localfuture = localpromise.get_future(); - if (localfuture.wait_for(std::chrono::milliseconds(5000)) == std::future_status::ready) - { - const webrtc::SessionDescriptionInterface *desc = localfuture.get(); - if (desc) + // waiting for answer + std::future localfuture = localpromise.get_future(); + if (localfuture.wait_for(std::chrono::milliseconds(5000)) == std::future_status::ready) { - answer = desc->Clone(); + const webrtc::SessionDescriptionInterface *desc = localfuture.get(); + if (desc) + { + answer = desc->Clone(); + } + else + { + RTC_LOG(LS_ERROR) << "Failed to create answer - no SDP"; + } } else { - RTC_LOG(LS_ERROR) << "Failed to create answer - no SDP"; + RTC_LOG(LS_ERROR) << "Failed to create answer - timeout"; + localSessionObserver->cancel(); } + } - else - { - RTC_LOG(LS_ERROR) << "Failed to create answer - timeout"; - localSessionObserver->cancel(); - } + } return answer; }