From f8048e13315e9cfdb155ccc014f3c5a468c23b73 Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Thu, 23 Jul 2026 12:42:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(WebRtcClient):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E5=90=8E=E8=B0=83=E7=94=A8=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=BC=95=E5=8F=91=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ttstd/controller/webrtc/WebRtcClient.java | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/WebRTCController/app/src/main/java/com/ttstd/controller/webrtc/WebRtcClient.java b/WebRTCController/app/src/main/java/com/ttstd/controller/webrtc/WebRtcClient.java index e224a4b..bf8c7cd 100644 --- a/WebRTCController/app/src/main/java/com/ttstd/controller/webrtc/WebRtcClient.java +++ b/WebRTCController/app/src/main/java/com/ttstd/controller/webrtc/WebRtcClient.java @@ -256,7 +256,7 @@ public class WebRtcClient { } public void sendControlCommand(ControlMessage message) { - if (dataChannel != null && dataChannel.state() == DataChannel.State.OPEN) { + if (isDataChannelOpen()) { DataChannel.Buffer buffer = new DataChannel.Buffer( ByteBuffer.wrap(message.toByteArray()), false); dataChannel.send(buffer); @@ -269,7 +269,7 @@ public class WebRtcClient { * fps <= 0 表示沿用当前帧率。 */ public void requestResolutionChange(int width, int height, int fps) { - if (dataChannel != null && dataChannel.state() == DataChannel.State.OPEN) { + if (isDataChannelOpen()) { ControlMessage msg = ControlMessage.newBuilder() .setAction(Action.SET_RESOLUTION) .setWidth(width) @@ -283,21 +283,28 @@ public class WebRtcClient { public void close() { if (remoteVideoTrack != null && remoteSurfaceView != null) { remoteVideoTrack.removeSink(remoteSurfaceView); + remoteVideoTrack = null; } if (dataChannel != null) { + try { dataChannel.unregisterObserver(); } catch (Exception ignored) {} dataChannel.close(); dataChannel.dispose(); + dataChannel = null; } if (videoDataChannel != null) { + try { videoDataChannel.unregisterObserver(); } catch (Exception ignored) {} videoDataChannel.close(); videoDataChannel.dispose(); + videoDataChannel = null; } if (peerConnection != null) { peerConnection.close(); peerConnection.dispose(); + peerConnection = null; } if (peerConnectionFactory != null) { peerConnectionFactory.dispose(); + peerConnectionFactory = null; } } @@ -317,7 +324,12 @@ public class WebRtcClient { @Override public void onStateChange() { - Log.d(TAG, "DataChannel state: " + dc.state()); + try { + DataChannel.State state = dc.state(); + Log.d(TAG, "DataChannel state: " + state); + } catch (IllegalStateException e) { + Log.w(TAG, "DataChannel state error (already disposed?)"); + } } @Override @@ -348,7 +360,12 @@ public class WebRtcClient { @Override public void onStateChange() { - Log.d(TAG, "Video DataChannel state: " + dc.state()); + try { + DataChannel.State state = dc.state(); + Log.d(TAG, "Video DataChannel state: " + state); + } catch (IllegalStateException e) { + Log.w(TAG, "Video DataChannel state error (already disposed?)"); + } } @Override @@ -369,7 +386,13 @@ public class WebRtcClient { } public boolean isVideoDataChannelOpen() { - return videoDataChannel != null && videoDataChannel.state() == DataChannel.State.OPEN; + DataChannel dc = videoDataChannel; + if (dc == null) return false; + try { + return dc.state() == DataChannel.State.OPEN; + } catch (IllegalStateException e) { + return false; + } } /** 请求被控端切换屏幕串流模式(WebRTC / 自编码)。 */ @@ -423,6 +446,12 @@ public class WebRtcClient { /// 控制 DataChannel 是否已打开(用于状态显示)。 public boolean isDataChannelOpen() { - return dataChannel != null && dataChannel.state() == DataChannel.State.OPEN; + DataChannel dc = dataChannel; + if (dc == null) return false; + try { + return dc.state() == DataChannel.State.OPEN; + } catch (IllegalStateException e) { + return false; + } } }