0

我想创建一个数据通道。但我在实现这一点时遇到了一些困难。我添加了在“调用方”运行的代码:

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;
    }

正如很多人所说,我在offer之前创建了数据通道。这个方法(见下一个代码)被多次调用。通道状态从 2 变为 3。

public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel){
        print("channel.state \(dataChannel.readyState.rawValue)");
    }

但是我需要在接收方做什么?因为那里什么都没有发生?我需要将数据通道绑定到接收器吗?如果是这样,我该怎么做?

4

1 回答 1

0

一旦您从调用方创建数据通道并发送报价,您应该通过从被调用方发送答案来建立对等连接。一旦建立了对等连接,一旦数据通道准备好进行数据传输,将在被调用方调用下面的对等连接委托。

- (void)peerConnection:(RTCPeerConnection*)peerConnection
didOpenDataChannel:(RTCDataChannel*)dataChannel

要检查 peerconnection 建立成功与否,您可以使用以下 peerconnection 委托检查 ICE 连接状态:

- (void)peerConnection:(RTCPeerConnection *)peerConnection
didChangeIceConnectionState:(RTCIceConnectionState)newState 

因此,如果 newState == RTCIceConnectionStateConnected,则意味着对等连接已完成,您应该得到 didOpenDataChannel 调用。

于 2017-03-14T16:09:32.753 回答