为什么我的 `flutter_webrtc` 对等连接总是失败?
Why is my `flutter_webrtc` peer connection always failing?
我无法建立 WebRTC 连接。我已经使用此信号流在多个 Web 应用程序上成功建立了 WebRTC 连接,但我陷入了困境。我正在使用 flutter_webrtc
包,这是我的信号流 -
// Caller initiates offer
void onConnectPressed() async {
final pc = await PeerConnection().pc;
await pc.createDataChannel("channel", RTCDataChannelInit());
final offer = await pc.createOffer();
await pc.setLocalDescription(offer);
final description = await pc.getLocalDescription();
// Transporting offer to the other user
final socket = await SocketConnection().socket;
socket.emit("callPeer", {
"peerId": peerIdController.text,
"signal": json.encode(description?.toMap())
});
}
// Callee creates answer
useEventSubscription("peerIsCalling", (data) async {
print("peerIsCalling");
final pc = await PeerConnection().pc;
final socket = await SocketConnection().socket;
final signal = jsonDecode(data["signal"]);
await pc.setRemoteDescription(
RTCSessionDescription(
signal["sdp"],
signal["type"],
),
);
final answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
final description = await pc.getLocalDescription();
// Transporting answer to the other user
print("answer call");
socket.emit("answerCall", {
"callerId": data["callerId"],
"signal": {
"sdp": description?.sdp,
"type": description?.type,
},
});
});
// Caller gets answered
useEventSubscription("callAnswered", (signal) async {
final pc = await PeerConnection().pc;
pc.setRemoteDescription(
RTCSessionDescription(
signal["sdp"],
signal["type"],
),
);
});
这是我的 WebRTC 对等连接配置 -
final pc = await createPeerConnection({
"bundlePolicy": "balanced",
"encodedInsertableStreams": false,
"iceCandidatePoolSize": 0,
"iceServers": [
{
"credential": "",
"urls": ["stun:stun.l.google.com:19302"],
"username": ""
},
{
"credential": "",
"urls": ["stun:global.stun.twilio.com:3478"],
"username": ""
}
],
"iceTransportPolicy": "all",
"rtcpMuxPolicy": "require",
"sdpSemantics": "unified-plan"
});
我正在听 RTCPeerConnectionState
是这样的 -
RTCPeerConnectionStateNew
到
RTCPeerConnectionStateConnecting
到
RTCPeerConnectionStateFailed
需要注意的一件事是我能够将此应用程序连接到在浏览器中创建的对等连接。
谢天谢地,我找到了解决办法。 问题是 我在 android 模拟器上测试这个,WebRTC 在 android 模拟器上似乎有问题 而且正如我所说,我不是在交换冰候选人
我无法建立 WebRTC 连接。我已经使用此信号流在多个 Web 应用程序上成功建立了 WebRTC 连接,但我陷入了困境。我正在使用 flutter_webrtc
包,这是我的信号流 -
// Caller initiates offer
void onConnectPressed() async {
final pc = await PeerConnection().pc;
await pc.createDataChannel("channel", RTCDataChannelInit());
final offer = await pc.createOffer();
await pc.setLocalDescription(offer);
final description = await pc.getLocalDescription();
// Transporting offer to the other user
final socket = await SocketConnection().socket;
socket.emit("callPeer", {
"peerId": peerIdController.text,
"signal": json.encode(description?.toMap())
});
}
// Callee creates answer
useEventSubscription("peerIsCalling", (data) async {
print("peerIsCalling");
final pc = await PeerConnection().pc;
final socket = await SocketConnection().socket;
final signal = jsonDecode(data["signal"]);
await pc.setRemoteDescription(
RTCSessionDescription(
signal["sdp"],
signal["type"],
),
);
final answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
final description = await pc.getLocalDescription();
// Transporting answer to the other user
print("answer call");
socket.emit("answerCall", {
"callerId": data["callerId"],
"signal": {
"sdp": description?.sdp,
"type": description?.type,
},
});
});
// Caller gets answered
useEventSubscription("callAnswered", (signal) async {
final pc = await PeerConnection().pc;
pc.setRemoteDescription(
RTCSessionDescription(
signal["sdp"],
signal["type"],
),
);
});
这是我的 WebRTC 对等连接配置 -
final pc = await createPeerConnection({
"bundlePolicy": "balanced",
"encodedInsertableStreams": false,
"iceCandidatePoolSize": 0,
"iceServers": [
{
"credential": "",
"urls": ["stun:stun.l.google.com:19302"],
"username": ""
},
{
"credential": "",
"urls": ["stun:global.stun.twilio.com:3478"],
"username": ""
}
],
"iceTransportPolicy": "all",
"rtcpMuxPolicy": "require",
"sdpSemantics": "unified-plan"
});
我正在听 RTCPeerConnectionState
是这样的 -
RTCPeerConnectionStateNew
到
RTCPeerConnectionStateConnecting
到
RTCPeerConnectionStateFailed
需要注意的一件事是我能够将此应用程序连接到在浏览器中创建的对等连接。
谢天谢地,我找到了解决办法。 问题是 我在 android 模拟器上测试这个,WebRTC 在 android 模拟器上似乎有问题 而且正如我所说,我不是在交换冰候选人