在iOS本机应用程序上未打开WebRTC数据通道
我尝试在iOS(10)应用程序中使用WebRTC数据通道实现数据传输功能。 当前尝试使用本地传输重新实现官方样本() 但我的数据频道从未打开过 这是我在Swift中的测试代码:在iOS本机应用程序上未打开WebRTC数据通道,ios,objective-c,iphone,swift,webrtc,Ios,Objective C,Iphone,Swift,Webrtc,我尝试在iOS(10)应用程序中使用WebRTC数据通道实现数据传输功能。 当前尝试使用本地传输重新实现官方样本() 但我的数据频道从未打开过 这是我在Swift中的测试代码: import Foundation import WebRTC import RealmSwift class StreamingAPIWrapper { internal static let sharedInstance = StreamingAPIWrapper() var localConne
import Foundation
import WebRTC
import RealmSwift
class StreamingAPIWrapper {
internal static let sharedInstance = StreamingAPIWrapper()
var localConnection: RTCPeerConnection?
var remoteConnection: RTCPeerConnection?
var sendChannel: RTCDataChannel?
var reciveChannel: RTCDataChannel?
let dataChannelDelegate = MyRTCDataChannelDelegate()
let factory = RTCPeerConnectionFactory()
class func initStreamingAPI() {
RTCInitializeSSL()
}
class func deinitStreamingAPI() {
RTCCleanupSSL()
}
func connect() {
let configuration = RTCConfiguration()
configuration.bundlePolicy = .balanced
let server = RTCIceServer(urlStrings: ["stun:stun.l.google.com:19302"])
configuration.iceServers = [server]
let mandatoryConstraints = ["OfferToReceiveAudio": "true", "OfferToReceiveVideo": "true"]
let optionalConstraints = [ "DtlsSrtpKeyAgreement": "true", "RtpDataChannels" : "true", "internalSctpDataChannels" : "true"]
let constraints = RTCMediaConstraints(mandatoryConstraints: mandatoryConstraints, optionalConstraints: optionalConstraints)
let localConnectionDelegate = MyRTCPeerConnectionDelegate()
localConnectionDelegate.onGenerateCandidate = { candidate in
self.remoteConnection!.add(candidate)
}
localConnection = factory.peerConnection(with: configuration,
constraints: constraints,
delegate: localConnectionDelegate)
print("Created local connection")
let remoteConnectionDelegate = MyRTCPeerConnectionDelegate()
remoteConnectionDelegate.onGenerateCandidate = { candidate in
self.localConnection!.add(candidate)
}
remoteConnection = factory.peerConnection(with: configuration,
constraints: constraints,
delegate: remoteConnectionDelegate)
print("Created remote connection")
localConnection?.offer(for: constraints, completionHandler: { (description1, _) in
self.localConnection?.setLocalDescription(description1!, completionHandler: { _ in })
self.remoteConnection?.setRemoteDescription(description1!, completionHandler: { _ in })
self.remoteConnection?.answer(for: constraints, completionHandler: { (description2, error) in
self.localConnection?.setRemoteDescription(description2!, completionHandler: { _ in })
self.remoteConnection?.setLocalDescription(description2!, completionHandler: { _ in })
})
})
let dataChannelConfiguration = RTCDataChannelConfiguration()
dataChannelConfiguration.isOrdered = true
sendChannel = localConnection?.dataChannel(forLabel: "test", configuration: dataChannelConfiguration)
}
}
class MyRTCPeerConnectionDelegate: NSObject, RTCPeerConnectionDelegate {
var onConnect: (() -> ())?
var onGenerateCandidate: ((RTCIceCandidate) -> ())?
var onOffer: (() -> ())?
func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) {
print("didAddStream")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) {
print("didRemoveStream")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) {
print("didOpenDataChannel")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate) {
onGenerateCandidate?(candidate)
print("didGenerateCandidate")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]) {
print("didRemoveCandidates")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState) {
print("didChangeIceGatheringState")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) {
print("didChangeIceConnectionState \(newState.rawValue)")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState) {
print("didChangeSignalingState \(stateChanged.rawValue)")
}
func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) {
print("shouldNegotiate")
}
}
class MyRTCDataChannelDelegate: NSObject, RTCDataChannelDelegate {
func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) {
print("didReciveMessage")
}
func dataChannel(_ dataChannel: RTCDataChannel, didChangeBufferedAmount amount: UInt64) {
print("didChangeAmount")
}
func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) {
print("didChangeDataChannel")
}
}
这将生成输出:
Created local connection
Created remote connection
didChangeSignalingState 1
didChangeSignalingState 3
shouldNegotiate
didChangeIceGatheringState
didGenerateCandidate
didGenerateCandidate
didGenerateCandidate
didGenerateCandidate
didChangeSignalingState 0
didChangeIceConnectionState 1
didChangeSignalingState 0
didChangeIceConnectionState 1
didChangeIceGatheringState
我还尝试了来自websockets服务器的代码,用于信令,但数据通道也没有打开。
我做错了什么?根据提供的代码,您正在创建报价后创建数据频道 将数据通道添加到peerConnection后创建优惠,
因此,报价/应答SDP将包含数据通道详细信息
RtpDataChannels
已弃用,因此无需在约束中指定。感谢您的支持。但是,如果我在创建peerconnection之后移动创建数据通道,那么我只有“shouldConfertive”委托调用创建的本地连接shouldConfertive创建的远程连接
您需要创建报价,然后是数据通道。您好,我可以在iOS 9上打开通道,但在iOS 10上仍然无法打开。有关于iOS 10支持的文件吗?似乎DataChannel在10上根本不起作用?