DataChannel Webrtc Swift

强颜欢笑 提交于 2019-12-23 04:29:11

问题


I want to create an data channel. But I'm having some difficulties to implement this. I have added the code that runs on the "caller side":

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

I create the data channel before the offer as said by many people. This method (see next code) is called several times. The channel state is going from 2 to 3.

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

But what do I need to do at the receiving side? Because nothing happens there? Do I need to bind the data channel to the receiver? If so, how can I do that?


回答1:


Once you create the data channel form caller side and send offer, you should establish the peer connection by sending the answer from the callee side. Once peer connection gets established, below peerconnection delegate will be called at callee side once data channel is ready for data transfer.

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

To check where peerconnection established successfully or not, you can check the ICE connection state using below peerconnection delegate:

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

So, if newState == RTCIceConnectionStateConnected, it means peer connection done and you should get didOpenDataChannel call.



来源:https://stackoverflow.com/questions/42788020/datachannel-webrtc-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!