Skip to content

Commit

Permalink
return an error when adding backend stream fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mpromonet committed Jan 4, 2025
1 parent 213f221 commit cc1ae8f
Showing 1 changed file with 80 additions and 78 deletions.
158 changes: 80 additions & 78 deletions src/PeerConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> peerlock(m_peerMapMutex);
m_peer_connectionobs_map.insert(std::pair<std::string, PeerConnectionObserver *>(peerid, peerConnectionObserver));
}
RTC_LOG(LS_ERROR) << "Can't add stream";
} else {
// register peerid
{
std::lock_guard<std::mutex> peerlock(m_peerMapMutex);
m_peer_connectionobs_map.insert(std::pair<std::string, PeerConnectionObserver *>(peerid, peerConnectionObserver));
}

// ask to create offer
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtcoptions;
rtcoptions.offer_to_receive_video = 0;
rtcoptions.offer_to_receive_audio = 0;
std::promise<const webrtc::SessionDescriptionInterface *> localpromise;
rtc::scoped_refptr<CreateSessionDescriptionObserver> localSessionObserver(CreateSessionDescriptionObserver::Create(peerConnection, localpromise));
peerConnection->CreateOffer(localSessionObserver.get(), rtcoptions);

// waiting for offer
std::future<const webrtc::SessionDescriptionInterface *> 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<const webrtc::SessionDescriptionInterface *> localpromise;
rtc::scoped_refptr<CreateSessionDescriptionObserver> localSessionObserver(CreateSessionDescriptionObserver::Create(peerConnection, localpromise));
peerConnection->CreateOffer(localSessionObserver.get(), rtcoptions);

// waiting for offer
std::future<const webrtc::SessionDescriptionInterface *> 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;
}
Expand Down Expand Up @@ -805,60 +805,62 @@ std::unique_ptr<webrtc::SessionDescriptionInterface> 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<const webrtc::SessionDescriptionInterface *> remotepromise;
rtc::scoped_refptr<SetSessionDescriptionObserver> remoteSessionObserver(SetSessionDescriptionObserver::Create(peerConnection, remotepromise));
peerConnection->SetRemoteDescription(remoteSessionObserver.get(), session_description);
// waiting for remote description
std::future<const webrtc::SessionDescriptionInterface *> 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<const webrtc::SessionDescriptionInterface *> remotepromise;
rtc::scoped_refptr<SetSessionDescriptionObserver> remoteSessionObserver(SetSessionDescriptionObserver::Create(peerConnection, remotepromise));
peerConnection->SetRemoteDescription(remoteSessionObserver.get(), session_description);
// waiting for remote description
std::future<const webrtc::SessionDescriptionInterface *> 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<const webrtc::SessionDescriptionInterface *> localpromise;
rtc::scoped_refptr<CreateSessionDescriptionObserver> 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<const webrtc::SessionDescriptionInterface *> localpromise;
rtc::scoped_refptr<CreateSessionDescriptionObserver> 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<const webrtc::SessionDescriptionInterface *> 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<const webrtc::SessionDescriptionInterface *> 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;
}
Expand Down

0 comments on commit cc1ae8f

Please sign in to comment.