| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.media; | 5 package org.chromium.media; |
| 6 | 6 |
| 7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
| 8 import android.app.Activity; | 8 import android.app.Activity; |
| 9 import android.app.Fragment; | 9 import android.app.Fragment; |
| 10 import android.app.FragmentManager; | 10 import android.app.FragmentManager; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 import android.media.projection.MediaProjectionManager; | 21 import android.media.projection.MediaProjectionManager; |
| 22 import android.os.Build; | 22 import android.os.Build; |
| 23 import android.os.Handler; | 23 import android.os.Handler; |
| 24 import android.os.HandlerThread; | 24 import android.os.HandlerThread; |
| 25 import android.util.DisplayMetrics; | 25 import android.util.DisplayMetrics; |
| 26 import android.view.Display; | 26 import android.view.Display; |
| 27 import android.view.Surface; | 27 import android.view.Surface; |
| 28 import android.view.WindowManager; | 28 import android.view.WindowManager; |
| 29 | 29 |
| 30 import org.chromium.base.ApplicationStatus; | 30 import org.chromium.base.ApplicationStatus; |
| 31 import org.chromium.base.ContextUtils; |
| 31 import org.chromium.base.Log; | 32 import org.chromium.base.Log; |
| 32 import org.chromium.base.annotations.CalledByNative; | 33 import org.chromium.base.annotations.CalledByNative; |
| 33 import org.chromium.base.annotations.JNINamespace; | 34 import org.chromium.base.annotations.JNINamespace; |
| 34 | 35 |
| 35 import java.nio.ByteBuffer; | 36 import java.nio.ByteBuffer; |
| 36 | 37 |
| 37 /** | 38 /** |
| 38 * This class implements Screen Capture using projection API, introduced in Andr
oid | 39 * This class implements Screen Capture using projection API, introduced in Andr
oid |
| 39 * API 21 (L Release). Capture takes place in the current Looper, while pixel | 40 * API 21 (L Release). Capture takes place in the current Looper, while pixel |
| 40 * download takes place in another thread used by ImageReader. | 41 * download takes place in another thread used by ImageReader. |
| 41 **/ | 42 **/ |
| 42 @JNINamespace("media") | 43 @JNINamespace("media") |
| 43 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 44 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 44 public class ScreenCapture extends Fragment { | 45 public class ScreenCapture extends Fragment { |
| 45 private static final String TAG = "cr_ScreenCapture"; | 46 private static final String TAG = "cr_ScreenCapture"; |
| 46 | 47 |
| 47 private static final int REQUEST_MEDIA_PROJECTION = 1; | 48 private static final int REQUEST_MEDIA_PROJECTION = 1; |
| 48 | 49 |
| 49 // Native callback context variable. | 50 // Native callback context variable. |
| 50 private final long mNativeScreenCaptureMachineAndroid; | 51 private final long mNativeScreenCaptureMachineAndroid; |
| 51 private final Context mContext; | |
| 52 | 52 |
| 53 private static enum CaptureState { ATTACHED, ALLOWED, STARTED, STOPPING, STO
PPED } | 53 private static enum CaptureState { ATTACHED, ALLOWED, STARTED, STOPPING, STO
PPED } |
| 54 private static enum DeviceOrientation { PORTRAIT, LANDSCAPE } | 54 private static enum DeviceOrientation { PORTRAIT, LANDSCAPE } |
| 55 private final Object mCaptureStateLock = new Object(); | 55 private final Object mCaptureStateLock = new Object(); |
| 56 private CaptureState mCaptureState = CaptureState.STOPPED; | 56 private CaptureState mCaptureState = CaptureState.STOPPED; |
| 57 | 57 |
| 58 private MediaProjection mMediaProjection; | 58 private MediaProjection mMediaProjection; |
| 59 private MediaProjectionManager mMediaProjectionManager; | 59 private MediaProjectionManager mMediaProjectionManager; |
| 60 private VirtualDisplay mVirtualDisplay; | 60 private VirtualDisplay mVirtualDisplay; |
| 61 private Surface mSurface; | 61 private Surface mSurface; |
| 62 private ImageReader mImageReader; | 62 private ImageReader mImageReader; |
| 63 private HandlerThread mThread; | 63 private HandlerThread mThread; |
| 64 private Handler mBackgroundHandler; | 64 private Handler mBackgroundHandler; |
| 65 private Display mDisplay; | 65 private Display mDisplay; |
| 66 private DeviceOrientation mCurrentOrientation; | 66 private DeviceOrientation mCurrentOrientation; |
| 67 private Intent mResultData; | 67 private Intent mResultData; |
| 68 | 68 |
| 69 private int mScreenDensity; | 69 private int mScreenDensity; |
| 70 private int mWidth; | 70 private int mWidth; |
| 71 private int mHeight; | 71 private int mHeight; |
| 72 private int mFormat; | 72 private int mFormat; |
| 73 private int mResultCode; | 73 private int mResultCode; |
| 74 | 74 |
| 75 ScreenCapture(Context context, long nativeScreenCaptureMachineAndroid) { | 75 ScreenCapture(long nativeScreenCaptureMachineAndroid) { |
| 76 mContext = context; | |
| 77 mNativeScreenCaptureMachineAndroid = nativeScreenCaptureMachineAndroid; | 76 mNativeScreenCaptureMachineAndroid = nativeScreenCaptureMachineAndroid; |
| 78 } | 77 } |
| 79 | 78 |
| 80 // Factory method. | 79 // Factory method. |
| 81 @CalledByNative | 80 @CalledByNative |
| 82 static ScreenCapture createScreenCaptureMachine( | 81 static ScreenCapture createScreenCaptureMachine(long nativeScreenCaptureMach
ineAndroid) { |
| 83 Context context, long nativeScreenCaptureMachineAndroid) { | |
| 84 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 82 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 85 return new ScreenCapture(context, nativeScreenCaptureMachineAndroid)
; | 83 return new ScreenCapture(nativeScreenCaptureMachineAndroid); |
| 86 } | 84 } |
| 87 return null; | 85 return null; |
| 88 } | 86 } |
| 89 | 87 |
| 90 // Internal class implementing the ImageReader listener. Gets pinged when a | 88 // Internal class implementing the ImageReader listener. Gets pinged when a |
| 91 // new frame is been captured and downloaded to memory-backed buffers. | 89 // new frame is been captured and downloaded to memory-backed buffers. |
| 92 private class CrImageReaderListener implements ImageReader.OnImageAvailableL
istener { | 90 private class CrImageReaderListener implements ImageReader.OnImageAvailableL
istener { |
| 93 @Override | 91 @Override |
| 94 public void onImageAvailable(ImageReader reader) { | 92 public void onImageAvailable(ImageReader reader) { |
| 95 synchronized (mCaptureStateLock) { | 93 synchronized (mCaptureStateLock) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 super.onDetach(); | 200 super.onDetach(); |
| 203 Log.d(TAG, "onDetach"); | 201 Log.d(TAG, "onDetach"); |
| 204 stopCapture(); | 202 stopCapture(); |
| 205 } | 203 } |
| 206 | 204 |
| 207 @CalledByNative | 205 @CalledByNative |
| 208 public boolean allocate(int width, int height) { | 206 public boolean allocate(int width, int height) { |
| 209 mWidth = width; | 207 mWidth = width; |
| 210 mHeight = height; | 208 mHeight = height; |
| 211 | 209 |
| 212 mMediaProjectionManager = (MediaProjectionManager) mContext.getSystemSer
vice( | 210 mMediaProjectionManager = |
| 213 Context.MEDIA_PROJECTION_SERVICE); | 211 (MediaProjectionManager) ContextUtils.getApplicationContext().ge
tSystemService( |
| 212 Context.MEDIA_PROJECTION_SERVICE); |
| 214 if (mMediaProjectionManager == null) { | 213 if (mMediaProjectionManager == null) { |
| 215 Log.e(TAG, "mMediaProjectionManager is null"); | 214 Log.e(TAG, "mMediaProjectionManager is null"); |
| 216 return false; | 215 return false; |
| 217 } | 216 } |
| 218 | 217 |
| 219 WindowManager windowManager = | 218 WindowManager windowManager = |
| 220 (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE
); | 219 (WindowManager) ContextUtils.getApplicationContext().getSystemSe
rvice( |
| 220 Context.WINDOW_SERVICE); |
| 221 mDisplay = windowManager.getDefaultDisplay(); | 221 mDisplay = windowManager.getDefaultDisplay(); |
| 222 | 222 |
| 223 DisplayMetrics metrics = new DisplayMetrics(); | 223 DisplayMetrics metrics = new DisplayMetrics(); |
| 224 mDisplay.getMetrics(metrics); | 224 mDisplay.getMetrics(metrics); |
| 225 mScreenDensity = metrics.densityDpi; | 225 mScreenDensity = metrics.densityDpi; |
| 226 | 226 |
| 227 return true; | 227 return true; |
| 228 } | 228 } |
| 229 | 229 |
| 230 @CalledByNative | 230 @CalledByNative |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 long timestamp); | 425 long timestamp); |
| 426 | 426 |
| 427 // Method for ScreenCapture implementations to notify activity result. | 427 // Method for ScreenCapture implementations to notify activity result. |
| 428 private native void nativeOnActivityResult( | 428 private native void nativeOnActivityResult( |
| 429 long nativeScreenCaptureMachineAndroid, boolean result); | 429 long nativeScreenCaptureMachineAndroid, boolean result); |
| 430 | 430 |
| 431 // Method for ScreenCapture implementations to notify orientation change. | 431 // Method for ScreenCapture implementations to notify orientation change. |
| 432 private native void nativeOnOrientationChange( | 432 private native void nativeOnOrientationChange( |
| 433 long nativeScreenCaptureMachineAndroid, int rotation); | 433 long nativeScreenCaptureMachineAndroid, int rotation); |
| 434 } | 434 } |
| OLD | NEW |