diff --git a/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java b/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java index 6125566..68886ce 100644 --- a/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java +++ b/WebRTCController/app/src/main/java/com/tt/controller/MainActivity.java @@ -91,7 +91,18 @@ public class MainActivity extends AppCompatActivity { // 初始化 EGL 和远端视频渲染 eglBase = EglBase.create(); - remoteVideoView.init(eglBase.getEglBaseContext(), null); + remoteVideoView.init(eglBase.getEglBaseContext(), new RendererCommon.RendererEvents() { + @Override + public void onFirstFrameRendered() { + Log.d(TAG, "First frame rendered"); + } + + @Override + public void onFrameResolutionChanged(int videoWidth, int videoHeight, int rotation) { + Log.d(TAG, "Video resolution changed: " + videoWidth + "x" + videoHeight + " rotation: " + rotation); + adjustVideoSize(videoWidth, videoHeight, rotation); + } + }); remoteVideoView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT); remoteVideoView.setEnableHardwareScaler(true); remoteVideoView.setZOrderMediaOverlay(true); @@ -112,6 +123,56 @@ public class MainActivity extends AppCompatActivity { updateUI(false); } + private void adjustVideoSize(int videoWidth, int videoHeight, int rotation) { + runOnUiThread(() -> { + if (videoWidth == 0 || videoHeight == 0) return; + + int containerWidth = controlPanel.getWidth(); + int containerHeight = controlPanel.getHeight(); + + if (containerWidth == 0 || containerHeight == 0) { + // 如果容器还没测量好,稍后再试 + controlPanel.postDelayed(() -> adjustVideoSize(videoWidth, videoHeight, rotation), 100); + return; + } + + float videoAspectRatio; + if (rotation == 90 || rotation == 270) { + videoAspectRatio = (float) videoHeight / videoWidth; + } else { + videoAspectRatio = (float) videoWidth / videoHeight; + } + + float containerAspectRatio = (float) containerWidth / containerHeight; + + int newWidth, newHeight; + if (videoAspectRatio > containerAspectRatio) { + // 视频更宽,以容器宽度为准 + newWidth = containerWidth; + newHeight = (int) (containerWidth / videoAspectRatio); + } else { + // 视频更高,以容器高度为准 + newWidth = (int) (containerHeight * videoAspectRatio); + newHeight = containerHeight; + } + + // 更新视频渲染视图大小 + FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) remoteVideoView.getLayoutParams(); + lp.width = newWidth; + lp.height = newHeight; + remoteVideoView.setLayoutParams(lp); + + // 更新触摸层大小,使其与视频区域完全重合 + FrameLayout.LayoutParams overlayLp = (FrameLayout.LayoutParams) touchOverlay.getLayoutParams(); + overlayLp.width = newWidth; + overlayLp.height = newHeight; + touchOverlay.setLayoutParams(overlayLp); + + Log.d(TAG, String.format("Adjusted video view size: %dx%d (video: %dx%d, rotation: %d, container: %dx%d)", + newWidth, newHeight, videoWidth, videoHeight, rotation, containerWidth, containerHeight)); + }); + } + private void connectToControlled() { String serverUrl = etServerUrl.getText().toString().trim(); targetDeviceId = etTargetDeviceId.getText().toString().trim(); diff --git a/WebRTCController/app/src/main/res/layout/activity_main.xml b/WebRTCController/app/src/main/res/layout/activity_main.xml index fbb44b8..2d7f2de 100644 --- a/WebRTCController/app/src/main/res/layout/activity_main.xml +++ b/WebRTCController/app/src/main/res/layout/activity_main.xml @@ -91,6 +91,7 @@ @@ -99,13 +100,15 @@ + android:layout_height="match_parent" + android:layout_gravity="center" />