| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import android.annotation.TargetApi; |
| 14 | import android.app.Activity; |
| 15 | import android.content.Context; |
| 16 | import android.content.Intent; |
| 17 | import android.hardware.display.DisplayManager; |
| 18 | import android.hardware.display.VirtualDisplay; |
| 19 | import android.media.projection.MediaProjection; |
| 20 | import android.media.projection.MediaProjectionManager; |
| 21 | import android.os.Build; |
| 22 | import androidx.annotation.Nullable; |
| 23 | import android.view.Surface; |
| 24 |
| 25 | import org.telegram.messenger.FileLog; |
| 26 |
| 27 | /** |
| 28 | * An implementation of VideoCapturer to capture the screen content as a video stream. |
| 29 | * Capturing is done by {@code MediaProjection} on a {@code SurfaceTexture}. We interact with this |
| 30 | * {@code SurfaceTexture} using a {@code SurfaceTextureHelper}. |
| 31 | * The {@code SurfaceTextureHelper} is created by the native code and passed to this capturer in |
| 32 | * {@code VideoCapturer.initialize()}. On receiving a new frame, this capturer passes it |
| 33 | * as a texture to the native code via {@code CapturerObserver.onFrameCaptured()}. This takes |
| 34 | * place on the HandlerThread of the given {@code SurfaceTextureHelper}. When done with each frame, |
| 35 | * the native code returns the buffer to the {@code SurfaceTextureHelper} to be used for new |
| 36 | * frames. At any time, at most one frame is being processed. |
| 37 | * |
| 38 | * @note This class is only supported on Android Lollipop and above. |
| 39 | */ |
| 40 | @TargetApi(21) |
| 41 | public class ScreenCapturerAndroid implements VideoCapturer, VideoSink { |
| 42 | private static final int DISPLAY_FLAGS = |
| 43 | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION; |
| 44 | // DPI for VirtualDisplay, does not seem to matter for us. |
| 45 | private static final int VIRTUAL_DISPLAY_DPI = 400; |
| 46 |
| 47 | private final Intent mediaProjectionPermissionResultData; |
| 48 | private final MediaProjection.Callback mediaProjectionCallback; |
| 49 |
| 50 | private int width; |
| 51 | private int height; |
| 52 | @Nullable private VirtualDisplay virtualDisplay; |
| 53 | @Nullable private SurfaceTextureHelper surfaceTextureHelper; |
| 54 | @Nullable private CapturerObserver capturerObserver; |
| 55 | private long numCapturedFrames; |
| 56 | @Nullable private MediaProjection mediaProjection; |
| 57 | private boolean isDisposed; |
| 58 | @Nullable private MediaProjectionManager mediaProjectionManager; |
| 59 |
| 60 | /** |
| 61 | * Constructs a new Screen Capturer. |
| 62 | * |
| 63 | * @param mediaProjectionPermissionResultData the result data of MediaProjection permission |
| 64 | * activity; the calling app must validate that result code is Activity.RESULT_OK before |
| 65 | * calling this method. |
| 66 | * @param mediaProjectionCallback MediaProjection callback to implement application specific |
| 67 | * logic in events such as when the user revokes a previously granted capture permission. |
| 68 | **/ |
| 69 | public ScreenCapturerAndroid(Intent mediaProjectionPermissionResultData, |
| 70 | MediaProjection.Callback mediaProjectionCallback) { |
| 71 | this.mediaProjectionPermissionResultData = mediaProjectionPermissionResultData; |
| 72 | this.mediaProjectionCallback = mediaProjectionCallback; |
| 73 | } |
| 74 |
| 75 | private void checkNotDisposed() { |
| 76 | if (isDisposed) { |
| 77 | throw new RuntimeException("capturer is disposed."); |
| 78 | } |
| 79 | } |
| 80 |
| 81 | @Nullable |
| 82 | public MediaProjection getMediaProjection() { |
| 83 | return mediaProjection; |
| 84 | } |
| 85 |
| 86 | @Override |
| 87 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 88 | @SuppressWarnings("NoSynchronizedMethodCheck") |
| 89 | public synchronized void initialize(final SurfaceTextureHelper surfaceTextureHelper, |
| 90 | final Context applicationContext, final CapturerObserver capturerObserver) { |
| 91 | checkNotDisposed(); |
| 92 |
| 93 | if (capturerObserver == null) { |
| 94 | throw new RuntimeException("capturerObserver not set."); |
| 95 | } |
| 96 | this.capturerObserver = capturerObserver; |
| 97 |
| 98 | if (surfaceTextureHelper == null) { |
| 99 | throw new RuntimeException("surfaceTextureHelper not set."); |
| 100 | } |
| 101 | this.surfaceTextureHelper = surfaceTextureHelper; |
| 102 |
| 103 | mediaProjectionManager = (MediaProjectionManager) applicationContext.getSystemService( |
| 104 | Context.MEDIA_PROJECTION_SERVICE); |
| 105 | } |
| 106 |
| 107 | @Override |
| 108 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 109 | @SuppressWarnings("NoSynchronizedMethodCheck") |
| 110 | public synchronized void startCapture( |
| 111 | final int width, final int height, final int ignoredFramerate) { |
| 112 | if (mediaProjection != null || mediaProjectionManager == null) { |
| 113 | return; |
| 114 | } |
| 115 | try { |
| 116 | checkNotDisposed(); |
| 117 |
| 118 | this.width = width; |
| 119 | this.height = height; |
| 120 |
| 121 | mediaProjection = mediaProjectionManager.getMediaProjection( |
| 122 | Activity.RESULT_OK, mediaProjectionPermissionResultData); |
| 123 |
| 124 | // Let MediaProjection callback use the SurfaceTextureHelper thread. |
| 125 | mediaProjection.registerCallback(mediaProjectionCallback, surfaceTextureHelper.getHandler()); |
| 126 |
| 127 | createVirtualDisplay(); |
| 128 | capturerObserver.onCapturerStarted(true); |
| 129 | surfaceTextureHelper.startListening(ScreenCapturerAndroid.this); |
| 130 | } catch (Throwable e) { |
| 131 | mediaProjectionCallback.onStop(); |
| 132 | FileLog.e(e); |
| 133 | } |
| 134 | } |
| 135 |
| 136 | @Override |
| 137 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 138 | @SuppressWarnings("NoSynchronizedMethodCheck") |
| 139 | public synchronized void stopCapture() { |
| 140 | checkNotDisposed(); |
| 141 | ThreadUtils.invokeAtFrontUninterruptibly(surfaceTextureHelper.getHandler(), () -> { |
| 142 | surfaceTextureHelper.stopListening(); |
| 143 | capturerObserver.onCapturerStopped(); |
| 144 |
| 145 | if (virtualDisplay != null) { |
| 146 | virtualDisplay.release(); |
| 147 | virtualDisplay = null; |
| 148 | } |
| 149 |
| 150 | if (mediaProjection != null) { |
| 151 | // Unregister the callback before stopping, otherwise the callback recursively |
| 152 | // calls this method. |
| 153 | mediaProjection.unregisterCallback(mediaProjectionCallback); |
| 154 | mediaProjection.stop(); |
| 155 | mediaProjection = null; |
| 156 | } |
| 157 | }); |
| 158 | } |
| 159 |
| 160 | @Override |
| 161 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 162 | @SuppressWarnings("NoSynchronizedMethodCheck") |
| 163 | public synchronized void dispose() { |
| 164 | isDisposed = true; |
| 165 | } |
| 166 |
| 167 | /** |
| 168 | * Changes output video format. This method can be used to scale the output |
| 169 | * video, or to change orientation when the captured screen is rotated for example. |
| 170 | * |
| 171 | * @param width new output video width |
| 172 | * @param height new output video height |
| 173 | * @param ignoredFramerate ignored |
| 174 | */ |
| 175 | @Override |
| 176 | // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression. |
| 177 | @SuppressWarnings("NoSynchronizedMethodCheck") |
| 178 | public synchronized void changeCaptureFormat( |
| 179 | final int width, final int height, final int ignoredFramerate) { |
| 180 | checkNotDisposed(); |
| 181 |
| 182 | this.width = width; |
| 183 | this.height = height; |
| 184 |
| 185 | if (virtualDisplay == null) { |
| 186 | // Capturer is stopped, the virtual display will be created in startCaptuer(). |
| 187 | return; |
| 188 | } |
| 189 |
| 190 | // Create a new virtual display on the surfaceTextureHelper thread to avoid interference |
| 191 | // with frame processing, which happens on the same thread (we serialize events by running |
| 192 | // them on the same thread). |
| 193 | ThreadUtils.invokeAtFrontUninterruptibly(surfaceTextureHelper.getHandler(), () -> { |
| 194 | if (virtualDisplay == null) { |
| 195 | return; |
| 196 | } |
| 197 | // Since Android 14 a MediaProjection allows only one createVirtualDisplay() call and |
| 198 | // re-creating it stops the projection, so resize the existing display instead |
| 199 | // (before Android 12 resizing can scale the captured screen incorrectly). |
| 200 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { |
| 201 | virtualDisplay.release(); |
| 202 | createVirtualDisplay(); |
| 203 | } else { |
| 204 | surfaceTextureHelper.setTextureSize(width, height); |
| 205 | virtualDisplay.resize(width, height, VIRTUAL_DISPLAY_DPI); |
| 206 | virtualDisplay.setSurface(new Surface(surfaceTextureHelper.getSurfaceTexture())); |
| 207 | } |
| 208 | }); |
| 209 | } |
| 210 |
| 211 | private void createVirtualDisplay() { |
| 212 | surfaceTextureHelper.setTextureSize(width, height); |
| 213 | try { |
| 214 | virtualDisplay = mediaProjection.createVirtualDisplay("WebRTC_ScreenCapture", width, height, |
| 215 | VIRTUAL_DISPLAY_DPI, DISPLAY_FLAGS, new Surface(surfaceTextureHelper.getSurfaceTexture()), |
| 216 | null /* callback */, null /* callback handler */); |
| 217 | } catch (Throwable e) { |
| 218 | FileLog.e(e); |
| 219 | } |
| 220 | } |
| 221 |
| 222 | // This is called on the internal looper thread of {@Code SurfaceTextureHelper}. |
| 223 | @Override |
| 224 | public void onFrame(VideoFrame frame) { |
| 225 | numCapturedFrames++; |
| 226 | capturerObserver.onFrameCaptured(frame); |
| 227 | } |
| 228 |
| 229 | @Override |
| 230 | public boolean isScreencast() { |
| 231 | return true; |
| 232 | } |
| 233 |
| 234 | public long getNumCapturedFrames() { |
| 235 | return numCapturedFrames; |
| 236 | } |
| 237 | } |
| 238 |