failed to use RTCDataChannel to send message

173 views
Skip to first unread message

gaox...@foxmail.com

unread,
May 4, 2017, 4:49:05 AM5/4/17
to discuss-webrtc
 I have already managed to share video and audio between peers however i can't find a way to send message between peers using RTCDataChannel. i guess the problem is that 
- (void)peerConnection:(RTCPeerConnection*)peerConnection
    didOpenDataChannel
:(RTCDataChannel*)dataChannel

is never called.what puzzled me is that RTCDataChannel state can changed to kRTCDataChannelStateOpen
- (void)channelDidChangeState:(RTCDataChannel*)channel{
   
NSLog(@"did change state %d",channel.state);
}
and sendData return Yes
if([_rtcChannel sendData:buffer]){
     
NSLog(@"send msg");//_rtcChannel was created before creating offer
}
Questions:
  1. When didOpenDataChannel will be called?
  2. How to send message between peers using RTCDataChannel?

Taylor Brandstetter

unread,
May 4, 2017, 1:07:38 PM5/4/17
to discuss-webrtc
I've seen this kind of question a lot, so I created a question/answer on StackOverflow that tries to explain how the whole data channel negotiation model works: http://stackoverflow.com/questions/43788872/how-are-data-channels-negotiated-between-two-peers-in-webrtc/43788873

Does this answer your questions?

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/feb9b325-1a77-49e5-aa3b-574d5d814215%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Archer Chen

unread,
May 4, 2017, 10:55:47 PM5/4/17
to discuss-webrtc
Hi, taylor

Thank for your answer.

I am try to create data channel using your explanation.

In web<chrome Version 58.0.3029.96 (64-bit)>,  in myrtclib.js I set up the 
    var data_constraint = {reliable :false};

    function createDataChannel(role) {
        try {
            sendDChannel = pc.createDataChannel("datachannel_"+room+role, data_constraint);
        } catch (e) {
            console.log('error creating data channel ' + e);
            return;
        }
        sendDChannel.onopen = onSendChannelStateChange;
        sendDChannel.onclose = onSendChannelStateChange;
    }

In my iOS client, I set up the 'RTCDataChannelConfiguration'

- (RTCConfiguration *)peerConnectionConfiguration

{

    RTCConfiguration * configuration = [[RTCConfiguration alloc] init];

    

    if (!mICEServers)

    {

        mICEServers = [NSMutableArray array];

        NSArray * iceServers = @[RTCSTUNServerURL];  // RTCSTUNServerURL = @"stun:stun.l.google.com:19302";

        [mICEServers addObject:[self defaultSTUNServer:iceServers]];

    }

    

    configuration.iceServers = mICEServers;

    return configuration;

}


- (RTCMediaConstraints *)peerConnectionMediaConstraints

{

    return [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:nil];

}

- (RTCPeerConnection *)createPeerConnectionWithDataChannelLabel:(NSString *)connectionId

{

    RTCConfiguration * configuration = [self peerConnectionConfiguration];

    RTCMediaConstraints * mediaConstraints = [self peerConnectionMediaConstraints];

    

    RTCPeerConnection * peerConnection = [mPeerFactory peerConnectionWithConfiguration:configuration constraints:mediaConstraints delegate:self];

    

    if (peerConnection)

    {

        NSLog(@"Create peer connection successfully for %@", connectionId);

        

        RTCDataChannelConfiguration * dataChannelConfiguration = [[RTCDataChannelConfiguration alloc] init];

        dataChannelConfiguration.channelId = 10000;

        dataChannelConfiguration.isNegotiated = YES;

        // connectionId keep the same with js <format : [NSString stringWithFormat:@"datachannel_%@caller", roomName]>

        RTCDataChannel *dataChannel = [peerConnection dataChannelForLabel:connectionId configuration:dataChannelConfiguration];

        

        if (dataChannel)

        {

            dataChannel.delegate = self;

            NSLog(@"Data channel status : %@", [dataChannel readyStatus]);

        }

        else

        {

            NSLog(@"Create data channel failed for %@", connectionId);

        }

    }

    else

    {

        NSLog(@"Create peer connection failed for %@", connectionId);

    }

    return peerConnection;

}


Now, issue is app crash when run to dataChannelForLabel:configuration: ?
If I don't set up the 
isNegotiated and channelId of dataChannelConfiguration in here, where can I set it? The property of RTCDataChannel is readonly.

Device info:
My mac: 10.12.4
xcode : 8.3.2

WebRTC framework verson: M58
git status
HEAD detached at 4e72b0f87

在 2017年5月5日星期五 UTC+8上午1:07:38,Taylor Brandstetter写道:
I've seen this kind of question a lot, so I created a question/answer on StackOverflow that tries to explain how the whole data channel negotiation model works: http://stackoverflow.com/questions/43788872/how-are-data-channels-negotiated-between-two-peers-in-webrtc/43788873

Does this answer your questions?
On Wed, May 3, 2017 at 7:14 AM, <gaox...@foxmail.com> wrote:
 I have already managed to share video and audio between peers however i can't find a way to send message between peers using RTCDataChannel. i guess the problem is that 
- (void)peerConnection:(RTCPeerConnection*)peerConnection
    didOpenDataChannel
:(RTCDataChannel*)dataChannel

is never called.what puzzled me is that RTCDataChannel state can changed to kRTCDataChannelStateOpen
- (void)channelDidChangeState:(RTCDataChannel*)channel{
   
NSLog(@"did change state %d",channel.state);
}
and sendData return Yes
if([_rtcChannel sendData:buffer]){
     
NSLog(@"send msg");//_rtcChannel was created before creating offer
}
Questions:
  1. When didOpenDataChannel will be called?
  2. How to send message between peers using RTCDataChannel?

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.

Archer Chen

unread,
May 4, 2017, 10:57:10 PM5/4/17
to discuss-webrtc
var data_constraint = {negotiated : true, id : 10000};

Setting is here.


在 2017年5月5日星期五 UTC+8上午10:55:47,Archer Chen写道:

Taylor Brandstetter

unread,
May 5, 2017, 4:16:17 AM5/5/17
to discuss-webrtc
The crash is tracked by this bug I never got around to fixing: https://bugs.chromium.org/p/webrtc/issues/detail?id=4619

But the real issue here is the is the channelId of 10000. I forgot to mention this, but the ID isn't just an arbitrary value; it's a 0-based SCTP stream ID, and it can only go as high as the number of negotiated SCTP streams. Which will be 1024 by default for Chrome, so 10000 is too high. I'll update the SO question to call this out.

To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/d599a3df-d1d9-4ffd-b37d-40818e63b1c2%40googlegroups.com.

Archer Chen

unread,
May 5, 2017, 4:25:37 AM5/5/17
to discuss-webrtc
@Taylor,
Thank you for your reply. Now it works, when the channelId is less than 1024. Thank you very much.

在 2017年5月5日星期五 UTC+8下午4:16:17,Taylor Brandstetter写道:

gaox...@foxmail.com

unread,
May 7, 2017, 9:25:05 PM5/7/17
to discuss-webrtc
Thank you very much.


On Friday, May 5, 2017 at 1:07:38 AM UTC+8, Taylor Brandstetter wrote:
I've seen this kind of question a lot, so I created a question/answer on StackOverflow that tries to explain how the whole data channel negotiation model works: http://stackoverflow.com/questions/43788872/how-are-data-channels-negotiated-between-two-peers-in-webrtc/43788873

Does this answer your questions?
On Wed, May 3, 2017 at 7:14 AM, <gaox...@foxmail.com> wrote:
 I have already managed to share video and audio between peers however i can't find a way to send message between peers using RTCDataChannel. i guess the problem is that 
- (void)peerConnection:(RTCPeerConnection*)peerConnection
    didOpenDataChannel
:(RTCDataChannel*)dataChannel

is never called.what puzzled me is that RTCDataChannel state can changed to kRTCDataChannelStateOpen
- (void)channelDidChangeState:(RTCDataChannel*)channel{
   
NSLog(@"did change state %d",channel.state);
}
and sendData return Yes
if([_rtcChannel sendData:buffer]){
     
NSLog(@"send msg");//_rtcChannel was created before creating offer
}
Questions:
  1. When didOpenDataChannel will be called?
  2. How to send message between peers using RTCDataChannel?

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages