Dev
Dev

Reputation: 3945

RTCDataChannel not working iOS

I'm using RTCDataChannel. But messages which i'm sending through the channel are not receiving at the other peer.

Here is the code:

        let audioConstraint : RTCPair = RTCPair(key: "OfferToReceiveAudio", value: "true")
        let videoConstraint : RTCPair = RTCPair(key: "OfferToReceiveVideo", value: "true")
        let dtlsConstraint : RTCPair = RTCPair(key: "DtlsSrtpKeyAgreement", value: "true")
        let mediaContraints : RTCMediaConstraints = RTCMediaConstraints(mandatoryConstraints: [audioConstraint, videoConstraint], optionalConstraints: [ dtlsConstraint])

        RTCPeerConnectionFactory.initializeSSL()
        peerConnection = peerConnectionFactory.peerConnectionWithICEServers(servers, constraints: mediaContraints, delegate: self)

        dataChannels = peerConnection?.createDataChannelWithLabel(channelName,config: nil)
        dataChannels?.delegate = self


        var message : NSData = NSData(base64EncodedString: "helloo")
        var buffer : RTCDataBuffer = RTCDataBuffer(data: message, isBinary: true)
        dataChannels?.sendData(buffer)

Upvotes: 2

Views: 2563

Answers (3)

Hihikomori
Hihikomori

Reputation: 990

To make it works I call this send routine:

 let buffer = RTCDataBuffer(data: data, isBinary: true)
 remoteDataChannel?.sendData(buffer)

from the main thread.

Upvotes: 0

user523234
user523234

Reputation: 14834

I had the same problem until I set the option not to nil. If I skipped the steamId, it would not send. Even though the channel is open.

RTCDataChannelInit *dataInit = [[RTCDataChannelInit alloc] init];
dataInit.isNegotiated = YES;
dataInit.isOrdered = YES;
dataInit.maxRetransmits = 30;
dataInit.maxRetransmitTimeMs = 30000;
dataInit.streamId = 12;  //important setting
self.dataChannel = [_peerConnection createDataChannelWithLabel:kRTCDataChannelLabel config:dataInit];
self.dataChannel.delegate = self;

Upvotes: 0

Sumaira
Sumaira

Reputation: 71

Have you resolved it? One of the two peers should create data channel and other should attach the received data channel object to its data channel object. The initiator should create datachannel before sending offer. Hope this might be helpful

Upvotes: 3

Related Questions