|
| 1 | +package com.cloudwebrtc.webrtc; |
| 2 | + |
| 3 | +import org.webrtc.ScreenCapturerAndroid; |
| 4 | +import org.webrtc.SurfaceTextureHelper; |
| 5 | +import org.webrtc.CapturerObserver; |
| 6 | +import org.webrtc.VideoFrame; |
| 7 | + |
| 8 | +import android.annotation.TargetApi; |
| 9 | +import android.content.Context; |
| 10 | +import android.content.Intent; |
| 11 | +import android.media.projection.MediaProjection; |
| 12 | +import android.util.Log; |
|
10000
13 | +import android.view.Surface; |
| 14 | +import android.view.WindowManager; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * An implementation of ScreenCapturerAndroid to capture the screen content while being aware of device orientation |
| 19 | + */ |
| 20 | +@TargetApi(21) |
| 21 | +public class OrientationAwareScreenCapturer |
| 22 | + extends ScreenCapturerAndroid { |
| 23 | + private Context applicationContext; |
| 24 | + private WindowManager windowManager; |
| 25 | + private int width; |
| 26 | + private int height; |
| 27 | + private boolean isPortrait; |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructs a new Screen Capturer. |
| 31 | + * |
| 32 | + * @param mediaProjectionPermissionResultData the result data of MediaProjection permission |
| 33 | + * activity; the calling app must validate that result code is Activity.RESULT_OK before |
| 34 | + * calling this method. |
| 35 | + * @param mediaProjectionCallback MediaProjection callback to implement application specific |
| 36 | + * logic in events such as when the user revokes a previously granted capture permission. |
| 37 | + **/ |
| 38 | + public OrientationAwareScreenCapturer(Intent mediaProjectionPermissionResultData, |
| 39 | + MediaProjection.Callback mediaProjectionCallback) { |
| 40 | + super(mediaProjectionPermissionResultData, mediaProjectionCallback); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public synchronized void initialize(SurfaceTextureHelper surfaceTextureHelper, Context applicationContext, CapturerObserver capturerObserver) { |
| 45 | + super.initialize(surfaceTextureHelper, applicationContext, capturerObserver); |
| 46 | + this.applicationContext = applicationContext; |
| 47 | + Log.d("OrientationAwareSC", "OrientationAwareScreenCapturer: initialized and orientation isPortrait? " + this.isPortrait); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public synchronized void startCapture(int width, int height, int ignoredFramerate) { |
| 52 | + this.windowManager = (WindowManager) applicationContext.getSystemService( |
| 53 | + Context.WINDOW_SERVICE); |
| 54 | + this.isPortrait = isDeviceOrientationPortrait(); |
| 55 | + if (this.isPortrait) { |
| 56 | + this.width = width; |
DD71
td> | 57 | + this.height = height; |
| 58 | + } else { |
| 59 | + this.height = width; |
| 60 | + this.width = height; |
| 61 | + } |
| 62 | + super.startCapture(width, height, ignoredFramerate); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onFrame(VideoFrame frame) { |
| 67 | + final boolean isOrientationPortrait = isDeviceOrientationPortrait(); |
| 68 | + if (isOrientationPortrait != this.isPortrait) { |
| 69 | + this.isPortrait = isOrientationPortrait; |
| 70 | + |
| 71 | + if (this.isPortrait) { |
| 72 | + super.changeCaptureFormat(this.width, this.height, 15); |
| 73 | + } else { |
| 74 | + super.changeCaptureFormat(this.height, this.width, 15); |
| 75 | + } |
| 76 | + } |
| 77 | + super.onFrame(frame); |
| 78 | + } |
| 79 | + |
| 80 | + private boolean isDeviceOrientationPortrait() { |
| 81 | + final int surfaceRotation = windowManager.getDefaultDisplay().getRotation(); |
| 82 | + |
| 83 | + return surfaceRotation != Surface.ROTATION_90 && surfaceRotation != Surface.ROTATION_270; |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments