Impelementation of RTCDataChannel of WebRTC in iOS

2020-03-13 03:19发布

I am using ISBX/apprtc-ios code for video chat implementation. This work perfect in iPhone and simulator. I want to send text/string data between two peers and I am using RTCDataChannel class.

Following is my implementation and I am not able to establish the connection. It always give the status kRTCDataChannelStateConnecting How can I get the RTCDataChannel connected? Is there any working implementation available for WebRTC RTCDataChannel for iOS?

- (void)createNewDataChannel {
    if (self.clientDataChannel) {
        switch(self.clientDataChannel.state) {
            case kRTCDataChannelStateConnecting:
                NSLog(@"kRTCDataChannelStateConnecting");
                break;
            case kRTCDataChannelStateOpen:
                NSLog(@"kRTCDataChannelStateOpen");
                break;
            case kRTCDataChannelStateClosing:
                NSLog(@"kRTCDataChannelStateClosing");
                break;
            case kRTCDataChannelStateClosed:
                NSLog(@"kRTCDataChannelStateClosed");
                break;
            default:
                NSLog(@"Unknown");
        }
        return;
    }
    if (self.peerConnection == nil) {
        NSLog(@"Peerconnection is nil");
    }

    RTCDataChannelInit *DataChannelInit = [[RTCDataChannelInit alloc] init];
    DataChannelInit.maxRetransmits = 0;
    DataChannelInit.isOrdered=false;
    DataChannelInit.maxRetransmitTimeMs = -1;
    DataChannelInit.isNegotiated = false;
    DataChannelInit.streamId = 25;
    RTCDataChannel *dataChannel =[_peerConnection createDataChannelWithLabel:@"commands" config:DataChannelInit];
    dataChannel.delegate=self;
    self.clientDataChannel = dataChannel;

    if (self.clientDataChannel == nil) {
        NSLog(@"Datachannel is nil");
    }
    else {
        NSLog(@"Datachannel is working");
    }
}

1条回答
仙女界的扛把子
2楼-- · 2020-03-13 04:01

I am able to send data through RTCDataChannel. What I did is before sending the offer. I created the RTCDataChannelInit with the below configuration.

RTCDataChannelInit *datainit = [[RTCDataChannelInit alloc] init];

datainit.isNegotiated = YES;

datainit.isOrdered = YES;

datainit.maxRetransmits = 30;

datainit.maxRetransmitTimeMs = 30000;

datainit.streamId = 1;
self.dataChannel = [_peerConnection createDataChannelWithLabel:@"commands" config:datainit];
self.dataChannel.delegate=self;

Once both the devices get connected, I checked the state in the delegate function. The state of the channel is open.

- (void)channelDidChangeState:(RTCDataChannel*)channel
{
    NSLog(@"channel.state %u",channel.state);
}

Then I send the data as per the below code:

RTCDataBuffer *buffer = [[RTCDataBuffer alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] isBinary:NO];
BOOL x = [self.dataChannel sendData:buffer];

The configuration I used was given here: https://groups.google.com/forum/#!searchin/discuss-webrtc/RTCDataChannel/discuss-webrtc/9NObqxnItCg/mRvXBIwkA7wJ

查看更多
登录 后发表回答