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