project: 增加web和adb控制,未测试
This commit is contained in:
@@ -68,6 +68,12 @@ public class WebRtcClient {
|
||||
private RemoteDisconnectCallback remoteDisconnectCallback;
|
||||
private boolean remoteDisconnectNotified = false;
|
||||
|
||||
// 关键修复:控制端(网页)会在被控端弹出"接受"对话框期间就把 ICE 候选发过来,
|
||||
// 此时 peerConnection 尚未创建 / 远端描述(offer)尚未设置,直接 addIceCandidate 会被丢弃。
|
||||
// 因此先把候选缓存起来,等 setRemoteDescription(offer) 完成后再统一 flush。
|
||||
private boolean remoteDescriptionSet = false;
|
||||
private final List<IceCandidate> pendingRemoteCandidates = new ArrayList<>();
|
||||
|
||||
public interface InputCommandCallback {
|
||||
void onCommandReceived(byte[] data);
|
||||
}
|
||||
@@ -162,6 +168,8 @@ public class WebRtcClient {
|
||||
}
|
||||
this.currentControllerId = controllerId;
|
||||
this.remoteDisconnectNotified = false;
|
||||
this.remoteDescriptionSet = false;
|
||||
this.pendingRemoteCandidates.clear();
|
||||
|
||||
List<PeerConnection.IceServer> iceServers = new ArrayList<>();
|
||||
// 建议:如果你有 TURN 服务器,请务必在此处添加,例如:
|
||||
@@ -237,6 +245,9 @@ public class WebRtcClient {
|
||||
|
||||
@Override
|
||||
public void onSetSuccess() {
|
||||
// 远端描述(offer)已就绪,先 flush 之前缓存的控制端候选。
|
||||
remoteDescriptionSet = true;
|
||||
flushPendingCandidates();
|
||||
// 创建 Answer
|
||||
MediaConstraints constraints = new MediaConstraints();
|
||||
peerConnection.createAnswer(new AppSdpObserver(new AppSdpObserver.SdpEventListener() {
|
||||
@@ -297,11 +308,24 @@ public class WebRtcClient {
|
||||
}
|
||||
|
||||
public void addIceCandidate(IceCandidate candidate) {
|
||||
if (peerConnection == null || !remoteDescriptionSet) {
|
||||
// 连接尚未就绪(还在等待用户接受 / 远端描述未设置),先缓存避免候选被丢弃。
|
||||
pendingRemoteCandidates.add(candidate);
|
||||
return;
|
||||
}
|
||||
if (peerConnection != null) {
|
||||
peerConnection.addIceCandidate(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
private void flushPendingCandidates() {
|
||||
if (peerConnection == null) return;
|
||||
for (IceCandidate c : pendingRemoteCandidates) {
|
||||
peerConnection.addIceCandidate(c);
|
||||
}
|
||||
pendingRemoteCandidates.clear();
|
||||
}
|
||||
|
||||
public void sendInputResponse(String responseJson) {
|
||||
if (dataChannel != null && dataChannel.state() == DataChannel.State.OPEN) {
|
||||
DataChannel.Buffer buffer = new DataChannel.Buffer(
|
||||
@@ -319,6 +343,8 @@ public class WebRtcClient {
|
||||
}
|
||||
|
||||
private void closeCurrentConnection() {
|
||||
remoteDescriptionSet = false;
|
||||
pendingRemoteCandidates.clear();
|
||||
if (dataChannel != null) {
|
||||
try {
|
||||
dataChannel.unregisterObserver();
|
||||
|
||||
Reference in New Issue
Block a user