use data channel to communicate video enabling status

pull/560/head
ryanzhao 4 years ago
parent 4e36052c6c
commit 0684e5250d

@ -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..."

@ -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)
}
}
}
}

@ -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])
}
}

Loading…
Cancel
Save