fix(WebRtcClient): 修复关闭后调用状态引发的异常

This commit is contained in:
2026-07-23 12:42:09 +08:00
parent 1800881219
commit f8048e1331

View File

@@ -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;
}
}
}