diff --git a/Session/Calls/CallVC.swift b/Session/Calls/CallVC.swift index d54af1878..f3d0f3923 100644 --- a/Session/Calls/CallVC.swift +++ b/Session/Calls/CallVC.swift @@ -189,7 +189,7 @@ final class CallVC : UIViewController, WebRTCSessionDelegate { Storage.write { transaction in self.webRTCSession.sendPreOffer(to: self.sessionID, using: transaction).done { self.webRTCSession.sendOffer(to: self.sessionID, using: transaction).retainUntilComplete() - } + }.retainUntilComplete() } answerButton.isHidden = true } @@ -274,7 +274,7 @@ final class CallVC : UIViewController, WebRTCSessionDelegate { } // MARK: Delegate - func webRTCDidConnected() { + func webRTCIsConnected() { DispatchQueue.main.async { self.callInfoLabel.text = "Connected" UIView.animate(withDuration: 0.5, delay: 1, options: [], animations: { @@ -286,6 +286,10 @@ final class CallVC : UIViewController, WebRTCSessionDelegate { } } + func isRemoteVideoDidChange(isEnabled: Bool) { + remoteVideoView.isHidden = !isEnabled + } + // MARK: Interaction func handleAnswerMessage(_ message: CallMessage) { callInfoLabel.text = "Connecting..." diff --git a/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift b/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift index 41a1e6aa5..2f22843ce 100644 --- a/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift +++ b/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift @@ -14,7 +14,7 @@ extension WebRTCSession: RTCDataChannelDelegate { } public func sendJSON(_ json: JSON) { - if let dataChannel = self.dataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) { + if let dataChannel = dataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) { let dataBuffer = RTCDataBuffer(data: jsonAsData, isBinary: false) dataChannel.sendData(dataBuffer) } @@ -27,8 +27,10 @@ extension WebRTCSession: RTCDataChannelDelegate { public func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) { print("[Calls] Data channel did receive data: \(buffer)") - if let json = try? JSONSerialization.jsonObject(with: buffer.data, options: [ .fragmentsAllowed ]) { - + if let json = try? JSONSerialization.jsonObject(with: buffer.data, options: [ .fragmentsAllowed ]) as? JSON { + if let isRemoteVideoEnabled = json["video"] as? Bool { + delegate?.isRemoteVideoDidChange(isEnabled: isRemoteVideoEnabled) + } } } } diff --git a/SessionMessagingKit/Calls/WebRTCSession.swift b/SessionMessagingKit/Calls/WebRTCSession.swift index b801dd825..e0e00ab30 100644 --- a/SessionMessagingKit/Calls/WebRTCSession.swift +++ b/SessionMessagingKit/Calls/WebRTCSession.swift @@ -4,7 +4,8 @@ import WebRTC public protocol WebRTCSessionDelegate : AnyObject { var videoCapturer: RTCVideoCapturer { get } - func webRTCDidConnected() + func webRTCIsConnected() + func isRemoteVideoDidChange(isEnabled: Bool) } /// See https://webrtc.org/getting-started/overview for more information. @@ -244,7 +245,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { public func peerConnection(_ peerConnection: RTCPeerConnection, didChange state: RTCIceConnectionState) { print("[Calls] ICE connection state changed to: \(state).") if state == .connected { - delegate?.webRTCDidConnected() + delegate?.webRTCIsConnected() } } @@ -290,9 +291,11 @@ extension WebRTCSession { public func turnOffVideo() { localVideoTrack.isEnabled = false + sendJSON(["video": false]) } - public func turnOnVideo() { + public func turnOnVideo() { localVideoTrack.isEnabled = true + sendJSON(["video": true]) } }