Be even more cautious when tearing down a PeerConnectionClient.

// FREEBIE
pull/1/head
Matthew Chen 9 years ago
parent a328759f0d
commit 98caeb6a03

@ -281,6 +281,7 @@ protocol CallServiceObserver: class {
peerConnectionClient.createSignalingDataChannel() peerConnectionClient.createSignalingDataChannel()
assert(self.peerConnectionClient == nil, "Unexpected PeerConnectionClient instance") assert(self.peerConnectionClient == nil, "Unexpected PeerConnectionClient instance")
Logger.debug("\(self.TAG) setting peerConnectionClient in \(#function)")
self.peerConnectionClient = peerConnectionClient self.peerConnectionClient = peerConnectionClient
return self.peerConnectionClient!.createOffer() return self.peerConnectionClient!.createOffer()
@ -428,6 +429,7 @@ protocol CallServiceObserver: class {
// FIXME for first time call recipients I think we'll see mic/camera permission requests here, // FIXME for first time call recipients I think we'll see mic/camera permission requests here,
// even though, from the users perspective, no incoming call is yet visible. // even though, from the users perspective, no incoming call is yet visible.
assert(self.peerConnectionClient == nil, "Unexpected PeerConnectionClient instance") assert(self.peerConnectionClient == nil, "Unexpected PeerConnectionClient instance")
Logger.debug("\(self.self.TAG) setting peerConnectionClient in \(#function)")
self.peerConnectionClient = PeerConnectionClient(iceServers: iceServers, delegate: self) self.peerConnectionClient = PeerConnectionClient(iceServers: iceServers, delegate: self)
let offerSessionDescription = RTCSessionDescription(type: .offer, sdp: callerSessionDescription) let offerSessionDescription = RTCSessionDescription(type: .offer, sdp: callerSessionDescription)
@ -921,18 +923,28 @@ protocol CallServiceObserver: class {
/** /**
* The connection has been established. The clients can now communicate. * The connection has been established. The clients can now communicate.
*/ */
internal func peerConnectionClientIceConnected(_ peerconnectionClient: PeerConnectionClient) { internal func peerConnectionClientIceConnected(_ peerConnectionClient: PeerConnectionClient) {
AssertIsOnMainThread() AssertIsOnMainThread()
if peerConnectionClient != self.peerConnectionClient {
Logger.debug("\(self.TAG) \(#function) Ignoring event from obsolete peerConnectionClient")
return
}
self.handleIceConnected() self.handleIceConnected()
} }
/** /**
* The connection failed to establish. The clients will not be able to communicate. * The connection failed to establish. The clients will not be able to communicate.
*/ */
internal func peerConnectionClientIceFailed(_ peerconnectionClient: PeerConnectionClient) { internal func peerConnectionClientIceFailed(_ peerConnectionClient: PeerConnectionClient) {
AssertIsOnMainThread() AssertIsOnMainThread()
if peerConnectionClient != self.peerConnectionClient {
Logger.debug("\(self.TAG) \(#function) Ignoring event from obsolete peerConnectionClient")
return
}
self.handleFailedCall(error: CallError.disconnected) self.handleFailedCall(error: CallError.disconnected)
} }
@ -941,31 +953,51 @@ protocol CallServiceObserver: class {
* reach the local client via the internet. The delegate must shuttle these IceCandates to the other (remote) client * reach the local client via the internet. The delegate must shuttle these IceCandates to the other (remote) client
* out of band, as part of establishing a connection over WebRTC. * out of band, as part of establishing a connection over WebRTC.
*/ */
internal func peerConnectionClient(_ peerconnectionClient: PeerConnectionClient, addedLocalIceCandidate iceCandidate: RTCIceCandidate) { internal func peerConnectionClient(_ peerConnectionClient: PeerConnectionClient, addedLocalIceCandidate iceCandidate: RTCIceCandidate) {
AssertIsOnMainThread() AssertIsOnMainThread()
if peerConnectionClient != self.peerConnectionClient {
Logger.debug("\(self.TAG) \(#function) Ignoring event from obsolete peerConnectionClient")
return
}
self.handleLocalAddedIceCandidate(iceCandidate) self.handleLocalAddedIceCandidate(iceCandidate)
} }
/** /**
* Once the peerconnection is established, we can receive messages via the data channel, and notify the delegate. * Once the peerconnection is established, we can receive messages via the data channel, and notify the delegate.
*/ */
internal func peerConnectionClient(_ peerconnectionClient: PeerConnectionClient, received dataChannelMessage: OWSWebRTCProtosData) { internal func peerConnectionClient(_ peerConnectionClient: PeerConnectionClient, received dataChannelMessage: OWSWebRTCProtosData) {
AssertIsOnMainThread() AssertIsOnMainThread()
if peerConnectionClient != self.peerConnectionClient {
Logger.debug("\(self.TAG) \(#function) Ignoring event from obsolete peerConnectionClient")
return
}
self.handleDataChannelMessage(dataChannelMessage) self.handleDataChannelMessage(dataChannelMessage)
} }
internal func peerConnectionClient(_ peerconnectionClient: PeerConnectionClient, didUpdateLocal videoTrack: RTCVideoTrack?) { internal func peerConnectionClient(_ peerConnectionClient: PeerConnectionClient, didUpdateLocal videoTrack: RTCVideoTrack?) {
AssertIsOnMainThread() AssertIsOnMainThread()
if peerConnectionClient != self.peerConnectionClient {
Logger.debug("\(self.TAG) \(#function) Ignoring event from obsolete peerConnectionClient")
return
}
self.localVideoTrack = videoTrack self.localVideoTrack = videoTrack
self.fireDidUpdateVideoTracks() self.fireDidUpdateVideoTracks()
} }
internal func peerConnectionClient(_ peerconnectionClient: PeerConnectionClient, didUpdateRemote videoTrack: RTCVideoTrack?) { internal func peerConnectionClient(_ peerConnectionClient: PeerConnectionClient, didUpdateRemote videoTrack: RTCVideoTrack?) {
AssertIsOnMainThread() AssertIsOnMainThread()
if peerConnectionClient != self.peerConnectionClient {
Logger.debug("\(self.TAG) \(#function) Ignoring event from obsolete peerConnectionClient")
return
}
self.remoteVideoTrack = videoTrack self.remoteVideoTrack = videoTrack
self.fireDidUpdateVideoTracks() self.fireDidUpdateVideoTracks()
} }
@ -1028,21 +1060,27 @@ protocol CallServiceObserver: class {
Logger.debug("\(TAG) in \(#function)") Logger.debug("\(TAG) in \(#function)")
PeerConnectionClient.stopAudioSession() self.localVideoTrack = nil
peerConnectionClient?.terminate() self.remoteVideoTrack = nil
self.fireDidUpdateVideoTracks()
peerConnectionClient = nil
localVideoTrack = nil localVideoTrack = nil
remoteVideoTrack = nil remoteVideoTrack = nil
isRemoteVideoEnabled = false isRemoteVideoEnabled = false
var peerConnectionClientCopy = peerConnectionClient
Logger.debug("\(TAG) setting peerConnectionClient in \(#function)")
peerConnectionClient = nil
PeerConnectionClient.stopAudioSession()
peerConnectionClientCopy?.terminate()
peerConnectionClientCopy = nil
call?.removeAllObservers() call?.removeAllObservers()
call = nil call = nil
thread = nil thread = nil
incomingCallPromise = nil incomingCallPromise = nil
sendIceUpdatesImmediately = true sendIceUpdatesImmediately = true
pendingIceUpdateMessages = [] pendingIceUpdateMessages = []
fireDidUpdateVideoTracks()
} }
// MARK: - CallObserver // MARK: - CallObserver

@ -423,10 +423,7 @@ class PeerConnectionClient: NSObject, RTCPeerConnectionDelegate, RTCDataChannelD
AssertIsOnMainThread() AssertIsOnMainThread()
Logger.debug("\(TAG) in \(#function)") Logger.debug("\(TAG) in \(#function)")
delegate.peerConnectionClient(self, didUpdateLocal: nil) PeerConnectionClient.signalingQueue.sync {
delegate.peerConnectionClient(self, didUpdateRemote: nil)
PeerConnectionClient.signalingQueue.async {
assert(self.peerConnection != nil) assert(self.peerConnection != nil)
self.terminateInternal() self.terminateInternal()
} }

Loading…
Cancel
Save