communicate video enabling status using data channel

pull/560/head
ryanzhao 4 years ago
parent 8a6be4fc5b
commit 80151acad2

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

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

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

Loading…
Cancel
Save