Merge branch 'google3' up to cl/358287309.

This commit is contained in:
hai007
2021-02-19 11:28:49 -08:00
3 changed files with 27 additions and 6 deletions
@@ -343,11 +343,15 @@ bool ConnectionFlow::TransitionState(State current_state, State new_state) {
}
bool ConnectionFlow::CloseLocked() {
NEARBY_LOG(INFO, "Closing WebRTC connection.");
if (state_ == State::kEnded) {
return false;
}
state_ = State::kEnded;
single_threaded_signaling_offloader_.Shutdown();
peer_connection_observer_.Shutdown();
if (peer_connection_) peer_connection_->Close();
data_channel_observer_.reset();
@@ -28,6 +28,10 @@ PeerConnectionObserverImpl::PeerConnectionObserverImpl(
: connection_flow_(connection_flow),
local_ice_candidate_listener_(std::move(local_ice_candidate_listener)) {}
PeerConnectionObserverImpl::~PeerConnectionObserverImpl() {
Shutdown();
}
void PeerConnectionObserverImpl::OnIceCandidate(
const webrtc::IceCandidateInterface* candidate) {
local_ice_candidate_listener_.local_ice_candidate_found_cb(candidate);
@@ -38,8 +42,10 @@ void PeerConnectionObserverImpl::OnSignalingChange(
NEARBY_LOG(INFO, "OnSignalingChange: %d", new_state);
OffloadFromSignalingThread([this, new_state]() {
if (new_state == webrtc::PeerConnectionInterface::SignalingState::kStable)
if (new_state == webrtc::PeerConnectionInterface::SignalingState::kStable &&
connection_flow_) {
connection_flow_->OnSignalingStable();
}
});
}
@@ -47,8 +53,10 @@ void PeerConnectionObserverImpl::OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
NEARBY_LOG(INFO, "OnDataChannel");
data_channel->RegisterObserver(
connection_flow_->CreateDataChannelObserver(data_channel));
if (connection_flow_) {
data_channel->RegisterObserver(
connection_flow_->CreateDataChannelObserver(data_channel));
}
}
void PeerConnectionObserverImpl::OnIceGatheringChange(
@@ -61,7 +69,9 @@ void PeerConnectionObserverImpl::OnConnectionChange(
NEARBY_LOG(INFO, "OnConnectionChange: %d", new_state);
OffloadFromSignalingThread([this, new_state]() {
connection_flow_->ProcessOnPeerConnectionChange(new_state);
if (connection_flow_) {
connection_flow_->ProcessOnPeerConnectionChange(new_state);
}
});
}
@@ -69,6 +79,11 @@ void PeerConnectionObserverImpl ::OnRenegotiationNeeded() {
NEARBY_LOG(INFO, "OnRenegotiationNeeded");
}
void PeerConnectionObserverImpl::Shutdown() {
single_threaded_signaling_offloader_.Shutdown();
connection_flow_ = nullptr;
}
void PeerConnectionObserverImpl::OffloadFromSignalingThread(Runnable runnable) {
single_threaded_signaling_offloader_.Execute(std::move(runnable));
}
@@ -28,10 +28,10 @@ class ConnectionFlow;
class PeerConnectionObserverImpl : public webrtc::PeerConnectionObserver {
public:
~PeerConnectionObserverImpl() override = default;
PeerConnectionObserverImpl(
ConnectionFlow* connection_flow,
LocalIceCandidateListener local_ice_candidate_listener);
~PeerConnectionObserverImpl() override;
// webrtc::PeerConnectionObserver:
void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
@@ -45,10 +45,12 @@ class PeerConnectionObserverImpl : public webrtc::PeerConnectionObserver {
webrtc::PeerConnectionInterface::PeerConnectionState new_state) override;
void OnRenegotiationNeeded() override;
void Shutdown();
private:
void OffloadFromSignalingThread(Runnable runnable);
ConnectionFlow* connection_flow_;
ConnectionFlow* volatile connection_flow_;
LocalIceCandidateListener local_ice_candidate_listener_;
SingleThreadExecutor single_threaded_signaling_offloader_;
};