在尝试使用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
这里有什么我想念的吗?似乎正在建立频道并添加媒体流(我只想要数据),但没有任何回调。我能启用更多日志记录吗?任何帮助将不胜感激!
答案 0 :(得分: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];
}
};
一旦我以这种方式构建它,数据通道回调就会按预期触发。
答案 1 :(得分:1)
DtlsSrtpKeyAgreement
和internalSctpDataChannels
需要加入可选约束而非强制性。
要设置数据通道,我也将RtpDataChannels
置于可选项中。