2

在尝试使用 Objective C API 建立 WebRTC 数据通道时,我无法实际捕获任何 RTCDataChannelDelegate 回调。关于对等连接,一切似乎都很好,但我只到了成功添加对等连接流的地步。

我的步骤大致是:

创建报价:

    _channel = [_connection createDataChannelWithLabel: @"offer-1"
                                                config: [[RTCDataChannelInit alloc] init]];
    _channel.delegate = _stateMachine;
   [_connection createOfferWithDelegate: _stateMachine constraints: [[RTCMediaConstraints alloc] init]];

客户端 1 的 SDP 被发送到客户端 2,其中创建了一个答案:

    [_connection setRemoteDescriptionWithDelegate: _stateMachine sessionDescription: [[RTCSessionDescription alloc] initWithType: @"offer" sdp: sdp]];
    [_connection createAnswerWithDelegate: _stateMachine constraints: [[RTCMediaConstraints alloc] init]];

客户端 2 的 SDP 被发送回客户端 1:

    [_connection setRemoteDescriptionWithDelegate: _stateMachine sessionDescription: [[RTCSessionDescription alloc] initWithType: @"answer" sdp: sdp]];

之后,我得到了添加信号稳定的媒体流。以前,在我的 POC 期间,我能够获得数据通道回调,但我不太确定我在这里缺少什么。

这是对等连接设置:

    RTCPeerConnectionFactory* _cfactory = [[RTCPeerConnectionFactory alloc] init];

    NSArray* mandatory = @[
                           [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:@"true"],
                           [[RTCPair alloc] initWithKey:@"internalSctpDataChannels" value:@"true"],
                           ];

    RTCMediaConstraints* pcConstraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints: mandatory
                                                                               optionalConstraints: nil];
    RTCICEServer* server = [[RTCICEServer alloc] initWithURI:[NSURL URLWithString:@"stun:stun.l.google.com:19302"]
                                                    username: @""
                                                    password: @""];
    NSArray* servers = @[server];

    _connection = [_cfactory peerConnectionWithICEServers: servers
                                              constraints: pcConstraints
                                                 delegate: _stateMachine];

我的状态机实现了以下所有方法:

@protocol DelegateAggregator
    <RTCPeerConnectionDelegate, RTCSessionDescriptionDelegate, RTCDataChannelDelegate, RTCStatsDelegate>
@end

我在这里缺少什么吗?似乎正在建立频道并添加了媒体流(我只想要数据),但没有任何回调。我可以启用更多日志记录吗?任何帮助将非常感激!

4

2 回答 2

2

所以事实证明,我如何构建答案的关键错误是在回调中启动 SDP 发送到客户端 1。我正在等待会话创建的回调到委托将我的答案发送回客户端 1。因为它原来,这太早了,候选人聚会还没有结束。我在要附加的委托中设置了回调,如下所示:

//WRONG
_tunnel.stateMachine.peerConnectionSessionCallback = ^(RTCPeerConnection* peerConnection, RTCSessionDescription* sdp, NSError* error) {
        SessionAnswer* answer = [[SessionAnswer alloc] init];
        answer.sdp = [sdp description];

        [queueManager sendEvent: answer];
    };

正确的方法是等待这个事件:

_tunnel.stateMachine.peerConnectionICEGatheringCallback = ^(RTCPeerConnection* peerConnection, RTCICEGatheringState newState) {
        if (newState == RTCICEGatheringComplete) {
            SessionAnswer* answer = [[SessionAnswer alloc] init];
            answer.sdp = [[peerConnection localDescription] description];

            [queueManager sendEvent: answer];
        }
    };

一旦我以这种方式构建它,数据通道回调就会按预期触发。

于 2014-11-16T21:07:01.750 回答
1

DtlsSrtpKeyAgreement并且internalSctpDataChannels需要放入可选约束而不是强制。

要设置数据通道,我也输入RtpDataChannels了可选的。

于 2014-11-13T21:58:57.187 回答