From 65317b242907e5ca97b60d821aaa6c246b0b7bec Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Tue, 14 Jul 2026 00:49:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A2=AB=E6=8E=A7=E7=AB=AF=E9=95=BF?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E4=B8=8D=E5=8A=A8=EF=BC=8C=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E7=AB=AF=E6=97=A0=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tt/controlled/webrtc/WebRtcClient.java | 25 +++++++++++++++++++ .../tt/controller/webrtc/WebRtcClient.java | 12 +++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/WebRTCControlled/app/src/main/java/com/tt/controlled/webrtc/WebRtcClient.java b/WebRTCControlled/app/src/main/java/com/tt/controlled/webrtc/WebRtcClient.java index 736778b..75b6bf3 100644 --- a/WebRTCControlled/app/src/main/java/com/tt/controlled/webrtc/WebRtcClient.java +++ b/WebRTCControlled/app/src/main/java/com/tt/controlled/webrtc/WebRtcClient.java @@ -51,6 +51,11 @@ public class WebRtcClient { private EglBase eglBase; private String currentControllerId; + private VideoCapturer videoCapturer; + private int videoWidth; + private int videoHeight; + private int videoFps; + private InputCommandCallback inputCallback; public interface InputCommandCallback { @@ -93,6 +98,11 @@ public class WebRtcClient { } public void setVideoCapturer(VideoCapturer capturer, int width, int height, int fps) { + this.videoCapturer = capturer; + this.videoWidth = width; + this.videoHeight = height; + this.videoFps = fps; + SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBase.getEglBaseContext()); // 对于屏幕共享,isScreencast 设为 true,这会优化细节保留 VideoSource videoSource = peerConnectionFactory.createVideoSource(true); @@ -152,6 +162,9 @@ public class WebRtcClient { @Override public void onIceConnectionChange(PeerConnection.IceConnectionState newState) { Log.d(TAG, "ICE connection state: " + newState); + if (newState == PeerConnection.IceConnectionState.CONNECTED) { + refreshVideoSource(); + } } @Override @@ -371,4 +384,16 @@ public class WebRtcClient { wsClient.sendMessage(msg); } + + private void refreshVideoSource() { + if (videoCapturer != null) { + try { + Log.d(TAG, "Refreshing video capture to force a frame update for new connection"); + videoCapturer.stopCapture(); + videoCapturer.startCapture(videoWidth, videoHeight, videoFps); + } catch (Exception e) { + Log.e(TAG, "Error refreshing video capture: " + e.getMessage()); + } + } + } } diff --git a/WebRTCController/app/src/main/java/com/tt/controller/webrtc/WebRtcClient.java b/WebRTCController/app/src/main/java/com/tt/controller/webrtc/WebRtcClient.java index 7365ab7..89fba72 100644 --- a/WebRTCController/app/src/main/java/com/tt/controller/webrtc/WebRtcClient.java +++ b/WebRTCController/app/src/main/java/com/tt/controller/webrtc/WebRtcClient.java @@ -106,6 +106,10 @@ public class WebRtcClient { config.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY; config.iceCandidatePoolSize = 10; + // 建议添加以下配置,增强复杂网络下的稳定性 + config.iceTransportsType = PeerConnection.IceTransportsType.ALL; + config.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.ENABLED; + PeerObserver peerObserver = new PeerObserver(new PeerObserver.PeerEventListener() { @Override public void onIceCandidate(IceCandidate candidate) { @@ -117,9 +121,13 @@ public class WebRtcClient { Log.d(TAG, "ICE connection state: " + newState); if (newState == PeerConnection.IceConnectionState.CONNECTED) { if (connectionListener != null) connectionListener.onConnectionEstablished(); - } else if (newState == PeerConnection.IceConnectionState.FAILED || - newState == PeerConnection.IceConnectionState.DISCONNECTED) { + } else if (newState == PeerConnection.IceConnectionState.DISCONNECTED) { + // DISCONNECTED 可能是暂时的,通常建议观察一段时间或尝试重放信令 + Log.w(TAG, "ICE Disconnected, waiting for recovery..."); + } else if (newState == PeerConnection.IceConnectionState.FAILED) { + // FAILED 通常意味着需要重新发起呼叫 if (connectionListener != null) connectionListener.onDisconnected(); + // 这里可以触发重新创建 Offer 的逻辑 } }