Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions indra/llwebrtc/llwebrtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,8 @@ LLWebRTCPeerConnectionImpl::LLWebRTCPeerConnectionImpl() :
mPeerConnection(nullptr),
mMute(MUTE_INITIAL),
mAnswerReceived(false),
mPeerConnectionState(webrtc::PeerConnectionInterface::PeerConnectionState::kNew),
mDisconnectCount(0),
mPendingJobs(0)
{
}
Expand Down Expand Up @@ -1237,11 +1239,15 @@ void LLWebRTCPeerConnectionImpl::OnIceGatheringChange(webrtc::PeerConnectionInte
}
}

static const webrtc::TimeDelta DISCONNECT_RENEGOTIATE_DELAY = webrtc::TimeDelta::Millis(10000);

// Called any time the PeerConnectionState changes.
void LLWebRTCPeerConnectionImpl::OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state)
{
RTC_LOG(LS_ERROR) << __FUNCTION__ << " Peer Connection State Change " << new_state;

mPeerConnectionState = new_state;

switch (new_state)
{
case webrtc::PeerConnectionInterface::PeerConnectionState::kConnected:
Expand All @@ -1257,13 +1263,32 @@ void LLWebRTCPeerConnectionImpl::OnConnectionChange(webrtc::PeerConnectionInterf
break;
}
case webrtc::PeerConnectionInterface::PeerConnectionState::kFailed:
case webrtc::PeerConnectionInterface::PeerConnectionState::kDisconnected:
{
for (auto &observer : mSignalingObserverList)
{
observer->OnRenegotiationNeeded();
}

break;
}
case webrtc::PeerConnectionInterface::PeerConnectionState::kDisconnected:
{
// Wait 10 seconds before renegotiating in case the connection recovers on its own.
// Use a sequence count so that only the most recent disconnect transition can trigger
// a renegotiation, avoiding stale delayed tasks from earlier disconnect/reconnect cycles.
uint32_t disconnect_count = ++mDisconnectCount;
mWebRTCImpl->PostDelayedSignalingTask(
[this, disconnect_count]()
{
if (disconnect_count == mDisconnectCount
&& mPeerConnectionState == webrtc::PeerConnectionInterface::PeerConnectionState::kDisconnected)
{
for (auto &observer : mSignalingObserverList)
{
observer->OnRenegotiationNeeded();
}
}
},
DISCONNECT_RENEGOTIATE_DELAY);
break;
}
default:
Expand Down
11 changes: 11 additions & 0 deletions indra/llwebrtc/llwebrtc_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@ class LLWebRTCImpl : public LLWebRTCDeviceInterface, public webrtc::AudioDeviceO
mSignalingThread->PostTask(std::move(task), location);
}

void PostDelayedSignalingTask(absl::AnyInvocable<void() &&> task,
webrtc::TimeDelta delay,
const webrtc::Location& location = webrtc::Location::Current())
{
mSignalingThread->PostDelayedTask(std::move(task), delay, location);
}

void PostNetworkTask(absl::AnyInvocable<void() &&> task,
const webrtc::Location& location = webrtc::Location::Current())
{
Expand Down Expand Up @@ -676,6 +683,10 @@ class LLWebRTCPeerConnectionImpl : public LLWebRTCPeerConnectionInterface,
std::vector<LLWebRTCDataObserver *> mDataObserverList;
webrtc::scoped_refptr<webrtc::DataChannelInterface> mDataChannel;

// connection state tracking for delayed renegotiation on disconnect
webrtc::PeerConnectionInterface::PeerConnectionState mPeerConnectionState;
uint32_t mDisconnectCount;

std::atomic<int> mPendingJobs;
};

Expand Down
Loading