From 9030710d9ddec9787e002eedf8f4d43c8de8b987 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Mon, 27 Sep 2021 15:08:01 +1000 Subject: [PATCH] add data channel --- Session.xcodeproj/project.pbxproj | 4 +++ .../Calls/WebRTCSession+DataChannel.swift | 34 +++++++++++++++++++ SessionMessagingKit/Calls/WebRTCSession.swift | 6 +++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 64bcdd459..d321930cd 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -140,6 +140,7 @@ 7B4C75CD26BB92060000AC89 /* DeletedMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */; }; 7BC01A3E241F40AB00BC7C55 /* NotificationServiceExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BC01A3D241F40AB00BC7C55 /* NotificationServiceExtension.swift */; }; 7BC01A42241F40AB00BC7C55 /* SessionNotificationServiceExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 7BC01A3B241F40AB00BC7C55 /* SessionNotificationServiceExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 7BCD116C27016062006330F1 /* WebRTCSession+DataChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BCD116B27016062006330F1 /* WebRTCSession+DataChannel.swift */; }; 7BDCFC08242186E700641C39 /* NotificationServiceExtensionContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BDCFC07242186E700641C39 /* NotificationServiceExtensionContext.swift */; }; 7BDCFC0B2421EB7600641C39 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = B6F509951AA53F760068F56A /* Localizable.strings */; }; 945AA2B82B621254F69FA9E8 /* Pods_SessionUtilitiesKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9117261809D69B3D7C26B8F1 /* Pods_SessionUtilitiesKit.framework */; }; @@ -1114,6 +1115,7 @@ 7BC01A3B241F40AB00BC7C55 /* SessionNotificationServiceExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = SessionNotificationServiceExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 7BC01A3D241F40AB00BC7C55 /* NotificationServiceExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationServiceExtension.swift; sourceTree = ""; }; 7BC01A3F241F40AB00BC7C55 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 7BCD116B27016062006330F1 /* WebRTCSession+DataChannel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WebRTCSession+DataChannel.swift"; sourceTree = ""; }; 7BDCFC0424206E7300641C39 /* SessionNotificationServiceExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SessionNotificationServiceExtension.entitlements; sourceTree = ""; }; 7BDCFC07242186E700641C39 /* NotificationServiceExtensionContext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationServiceExtensionContext.swift; sourceTree = ""; }; 7DD180F770F8518B4E8796F2 /* Pods-SessionUtilitiesKit.app store release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SessionUtilitiesKit.app store release.xcconfig"; path = "Pods/Target Support Files/Pods-SessionUtilitiesKit/Pods-SessionUtilitiesKit.app store release.xcconfig"; sourceTree = ""; }; @@ -2364,6 +2366,7 @@ B806ECA026C4A7E4008BDA44 /* WebRTCSession+UI.swift */, B8B558FE26C4E05E00693325 /* WebRTCSession+MessageHandling.swift */, B8BF43B926CC95FB007828D1 /* WebRTC+Utilities.swift */, + 7BCD116B27016062006330F1 /* WebRTCSession+DataChannel.swift */, ); path = Calls; sourceTree = ""; @@ -4757,6 +4760,7 @@ C32C5FD6256E0346003C73A2 /* Notification+Thread.swift in Sources */, C3BBE0C72554F1570050F1E3 /* FixedWidthInteger+BigEndian.swift in Sources */, B806ECA126C4A7E4008BDA44 /* WebRTCSession+UI.swift in Sources */, + 7BCD116C27016062006330F1 /* WebRTCSession+DataChannel.swift in Sources */, C32C5C88256DD0D2003C73A2 /* Storage+Messaging.swift in Sources */, C32C59C7256DB41F003C73A2 /* TSThread.m in Sources */, C300A5B22554AF9800555489 /* VisibleMessage+Profile.swift in Sources */, diff --git a/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift b/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift new file mode 100644 index 000000000..41a1e6aa5 --- /dev/null +++ b/SessionMessagingKit/Calls/WebRTCSession+DataChannel.swift @@ -0,0 +1,34 @@ +import WebRTC +import Foundation + +extension WebRTCSession: RTCDataChannelDelegate { + + internal func createDataChannel() { + 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 + } + + public func sendJSON(_ json: JSON) { + if let dataChannel = self.dataChannel, let jsonAsData = try? JSONSerialization.data(withJSONObject: json, options: [ .fragmentsAllowed ]) { + let dataBuffer = RTCDataBuffer(data: jsonAsData, isBinary: false) + dataChannel.sendData(dataBuffer) + } + } + + // MARK: Data channel delegate + public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) { + print("[Calls] Data channed did change to \(dataChannel.readyState)") + } + + 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 ]) { + + } + } +} diff --git a/SessionMessagingKit/Calls/WebRTCSession.swift b/SessionMessagingKit/Calls/WebRTCSession.swift index fab40d43d..d4ebd7379 100644 --- a/SessionMessagingKit/Calls/WebRTCSession.swift +++ b/SessionMessagingKit/Calls/WebRTCSession.swift @@ -69,6 +69,9 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { return peerConnection.transceivers.first { $0.mediaType == .video }?.receiver.track as? RTCVideoTrack }() + // Data Channel + internal var dataChannel: RTCDataChannel? + // MARK: Error public enum Error : LocalizedError { case noThread @@ -87,6 +90,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { self.contactSessionID = contactSessionID super.init() let mediaStreamTrackIDS = ["ARDAMS"] + createDataChannel() peerConnection.add(audioTrack, streamIds: mediaStreamTrackIDS) peerConnection.add(localVideoTrack, streamIds: mediaStreamTrackIDS) // Configure audio session @@ -194,7 +198,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { peerConnection.close() } - // MARK: Delegate + // MARK: Peer connection delegate public func peerConnection(_ peerConnection: RTCPeerConnection, didChange state: RTCSignalingState) { print("[Calls] Signaling state changed to: \(state).") }