我想创建一个数据频道。但是我很难实施此功能。我添加了在"呼叫者侧"上运行的代码:
func initWebRTC() {
RTCInitializeSSL()
peerConnectionFactory = RTCPeerConnectionFactory()
let mandatoryConstraints = ["OfferToReceiveAudio": "true", "OfferToReceiveVideo": "false"]
let optionalConstraints = [ "DtlsSrtpKeyAgreement": "true", "RtpDataChannels" : "true"]
mediaConstraints = RTCMediaConstraints.init(mandatoryConstraints: mandatoryConstraints, optionalConstraints: optionalConstraints)
}
func prepareNewConnection() -> RTCPeerConnection {
var icsServers: [RTCIceServer] = []
icsServers.append(RTCIceServer(urlStrings: ["stun:stun.l.google.com:19302"], username:"",credential: ""))
let rtcConfig: RTCConfiguration = RTCConfiguration()
rtcConfig.tcpCandidatePolicy = RTCTcpCandidatePolicy.disabled
rtcConfig.bundlePolicy = RTCBundlePolicy.maxBundle
rtcConfig.rtcpMuxPolicy = RTCRtcpMuxPolicy.require
rtcConfig.iceServers = icsServers;
peerConnection = peerConnectionFactory.peerConnection(with: rtcConfig, constraints: mediaConstraints, delegate: self)
peerConnection.add(mediaStream);
let tt = RTCDataChannelConfiguration();
tt.isOrdered = false;
tt.isNegotiated = false
self.dataChannel = peerConnection.dataChannel(forLabel: "datachannel", configuration: tt)
self.dataChannel.delegate = self
print("create datachannel")
return peerConnection;
}
我在报价之前创建数据渠道,如许多人所说。此方法(请参阅下一个代码)被调用多次。频道状态从2到3。
public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel){
print("channel.state (dataChannel.readyState.rawValue)");
}
但是我需要在接收方面做什么?因为那里什么都没发生?我需要将数据通道绑定到接收器吗?如果是这样,我该怎么做?
创建数据通道表单呼叫者并发送报价后,您应该通过从Callee侧发送答案来建立对等连接。一旦建立对等连接,一旦数据频道准备好数据传输,在PeerConnection代表下面将在Callee侧调用。
- (void)peerConnection:(RTCPeerConnection*)peerConnection
didOpenDataChannel:(RTCDataChannel*)dataChannel
要检查PeerConnection是否成功地建立的位置,您可以使用下面的PeerConnection委托检查冰连接状态:
- (void)peerConnection:(RTCPeerConnection *)peerConnection
didChangeIceConnectionState:(RTCIceConnectionState)newState
因此,如果newState == rtciceconnectionstateconted,则表示对等连接,并且您应该接到didopendatachannel呼叫。