fix: 修复SurfaceViewRenderer显示画面不全

This commit is contained in:
2026-07-14 00:28:56 +08:00
parent b2af050e5d
commit 86eaec3370
2 changed files with 66 additions and 2 deletions

View File

@@ -91,7 +91,18 @@ public class MainActivity extends AppCompatActivity {
// 初始化 EGL 和远端视频渲染 // 初始化 EGL 和远端视频渲染
eglBase = EglBase.create(); 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.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
remoteVideoView.setEnableHardwareScaler(true); remoteVideoView.setEnableHardwareScaler(true);
remoteVideoView.setZOrderMediaOverlay(true); remoteVideoView.setZOrderMediaOverlay(true);
@@ -112,6 +123,56 @@ public class MainActivity extends AppCompatActivity {
updateUI(false); 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() { private void connectToControlled() {
String serverUrl = etServerUrl.getText().toString().trim(); String serverUrl = etServerUrl.getText().toString().trim();
targetDeviceId = etTargetDeviceId.getText().toString().trim(); targetDeviceId = etTargetDeviceId.getText().toString().trim();

View File

@@ -91,6 +91,7 @@
<!-- 控制面板(连接后显示) --> <!-- 控制面板(连接后显示) -->
<FrameLayout <FrameLayout
android:id="@+id/control_panel" android:id="@+id/control_panel"
android:background="#000000"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:visibility="gone"> android:visibility="gone">
@@ -99,13 +100,15 @@
<org.webrtc.SurfaceViewRenderer <org.webrtc.SurfaceViewRenderer
android:id="@+id/remote_video_view" android:id="@+id/remote_video_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
android:layout_gravity="center" />
<!-- 触摸控制层(覆盖在视频上方) --> <!-- 触摸控制层(覆盖在视频上方) -->
<com.tt.controller.view.RemoteTouchView <com.tt.controller.view.RemoteTouchView
android:id="@+id/touch_overlay" android:id="@+id/touch_overlay"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@android:color/transparent" /> android:background="@android:color/transparent" />
<!-- 顶部状态栏 --> <!-- 顶部状态栏 -->