communicate video enabling status using data channel

This commit is contained in:
ryanzhao 2021-10-13 14:42:36 +11:00
parent 8a6be4fc5b
commit 80151acad2
3 changed files with 23 additions and 14 deletions

View File

@ -37,6 +37,7 @@ final class CallVC : UIViewController, WebRTCSessionDelegate {
private lazy var remoteVideoView: RTCMTLVideoView = {
let result = RTCMTLVideoView()
result.alpha = 0
result.contentMode = .scaleAspectFill
return result
}()
@ -293,7 +294,11 @@ final class CallVC : UIViewController, WebRTCSessionDelegate {
}
func isRemoteVideoDidChange(isEnabled: Bool) {
remoteVideoView.isHidden = !isEnabled
DispatchQueue.main.async {
UIView.animate(withDuration: 0.25) {
self.remoteVideoView.alpha = isEnabled ? 1 : 0
}
}
}
// MARK: Interaction

View File

@ -3,18 +3,17 @@ import Foundation
extension WebRTCSession: RTCDataChannelDelegate {
internal func createDataChannel() {
internal func createDataChannel() -> RTCDataChannel? {
let dataChannelConfiguration = RTCDataChannelConfiguration()
dataChannelConfiguration.isOrdered = true
dataChannelConfiguration.isNegotiated = true
dataChannelConfiguration.maxRetransmits = 30
dataChannelConfiguration.maxPacketLifeTime = 30000
dataChannel = peerConnection.dataChannel(forLabel: "DATACHANNEL", configuration: dataChannelConfiguration)
dataChannel?.delegate = self
guard let dataChannel = peerConnection.dataChannel(forLabel: "VIDEOCONTROL", configuration: dataChannelConfiguration) else {
print("[Calls] Couldn't create data channel.")
return nil
}
return dataChannel
}
public func sendJSON(_ json: JSON) {
if let dataChannel = dataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) {
if let dataChannel = remoteDataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) {
let dataBuffer = RTCDataBuffer(data: jsonAsData, isBinary: false)
dataChannel.sendData(dataBuffer)
}
@ -22,7 +21,7 @@ extension WebRTCSession: RTCDataChannelDelegate {
// MARK: Data channel delegate
public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) {
print("[Calls] Data channed did change to \(dataChannel.readyState)")
print("[Calls] Data channel did change to \(dataChannel.readyState)")
}
public func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) {

View File

@ -74,7 +74,8 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
}()
// Data Channel
internal var dataChannel: RTCDataChannel?
internal var localDataChannel: RTCDataChannel?
internal var remoteDataChannel: RTCDataChannel?
// MARK: Error
public enum Error : LocalizedError {
@ -99,12 +100,17 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
peerConnection.add(localVideoTrack, streamIds: mediaStreamTrackIDS)
// Configure audio session
configureAudioSession()
// Data channel
if let dataChannel = createDataChannel() {
dataChannel.delegate = self
self.localDataChannel = dataChannel
}
}
// MARK: Signaling
public func sendPreOffer(to sessionID: String, using transaction: YapDatabaseReadWriteTransaction) -> Promise<Void> {
print("[Calls] Sending pre-offer message.")
createDataChannel()
guard let thread = TSContactThread.fetch(for: sessionID, using: transaction) else { return Promise(error: Error.noThread) }
let (promise, seal) = Promise<Void>.pending()
DispatchQueue.main.async {
@ -267,8 +273,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
public func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) {
print("[Calls] Data channel opened.")
self.dataChannel = dataChannel
self.dataChannel?.delegate = self
self.remoteDataChannel = dataChannel
}
}