From dbaf404ea287a06f7dbfda82dbb419d3ba7ec4ed Mon Sep 17 00:00:00 2001 From: gongluck <1039994845@qq.com> Date: Thu, 7 Nov 2024 17:32:55 +0800 Subject: [PATCH] update file ScreenCapturerAndroid.java from official commit https://webrtc-review.googlesource.com/c/src/+/305560 --- .../org/webrtc/ScreenCapturerAndroid.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/stream-webrtc-android/src/main/java/org/webrtc/ScreenCapturerAndroid.java b/stream-webrtc-android/src/main/java/org/webrtc/ScreenCapturerAndroid.java index 231d50715..08b03bd68 100644 --- a/stream-webrtc-android/src/main/java/org/webrtc/ScreenCapturerAndroid.java +++ b/stream-webrtc-android/src/main/java/org/webrtc/ScreenCapturerAndroid.java @@ -17,6 +17,8 @@ import android.hardware.display.VirtualDisplay; import android.media.projection.MediaProjection; import android.media.projection.MediaProjectionManager; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; import android.view.Surface; import androidx.annotation.Nullable; @@ -113,7 +115,7 @@ public synchronized void startCapture( // Let MediaProjection callback use the SurfaceTextureHelper thread. mediaProjection.registerCallback(mediaProjectionCallback, surfaceTextureHelper.getHandler()); - createVirtualDisplay(); + updateVirtualDisplay(); capturerObserver.onCapturerStarted(true); surfaceTextureHelper.startListening(ScreenCapturerAndroid.this); } @@ -171,24 +173,33 @@ public synchronized void changeCaptureFormat( this.height = height; if (virtualDisplay == null) { - // Capturer is stopped, the virtual display will be created in startCaptuer(). + // Capturer is stopped, the virtual display will be created in startCapture(). return; } // Create a new virtual display on the surfaceTextureHelper thread to avoid interference // with frame processing, which happens on the same thread (we serialize events by running // them on the same thread). - ThreadUtils.invokeAtFrontUninterruptibly(surfaceTextureHelper.getHandler(), new Runnable() { - @Override - public void run() { - virtualDisplay.release(); - createVirtualDisplay(); - } - }); + ThreadUtils.invokeAtFrontUninterruptibly( + surfaceTextureHelper.getHandler(), this::updateVirtualDisplay); } - private void createVirtualDisplay() { + private void updateVirtualDisplay() { surfaceTextureHelper.setTextureSize(width, height); + // Before Android S (12), resizing the virtual display can cause the captured screen to be + // scaled incorrectly, so keep the behavior of recreating the virtual display prior to Android + // S. + if (virtualDisplay == null || VERSION.SDK_INT < VERSION_CODES.S) { + createVirtualDisplay(); + } else { + virtualDisplay.resize(width, height, VIRTUAL_DISPLAY_DPI); + virtualDisplay.setSurface(new Surface(surfaceTextureHelper.getSurfaceTexture())); + } + } + private void createVirtualDisplay() { + if (virtualDisplay != null) { + virtualDisplay.release(); + } virtualDisplay = mediaProjection.createVirtualDisplay("WebRTC_ScreenCapture", width, height, VIRTUAL_DISPLAY_DPI, DISPLAY_FLAGS, new Surface(surfaceTextureHelper.getSurfaceTexture()), null /* callback */, null /* callback handler */);