Chromium Code Reviews| Index: media/base/android/java/src/org/chromium/media/ScreenCapture.java |
| diff --git a/media/base/android/java/src/org/chromium/media/ScreenCapture.java b/media/base/android/java/src/org/chromium/media/ScreenCapture.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c33e06c34ce4c1f44be08f362451fc225c1bd78d |
| --- /dev/null |
| +++ b/media/base/android/java/src/org/chromium/media/ScreenCapture.java |
| @@ -0,0 +1,281 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
|
mcasas
2016/04/27 01:19:26
2016
miu
2016/04/27 05:24:53
Hmm...these files are so far away (in the director
braveyao
2016/05/04 18:49:40
Done.
braveyao
2016/05/04 18:49:40
It seems like a common practice to gather all the
mcasas
2016/05/04 19:56:47
What do you mean "once all the Java files ..."
whywhat
2016/05/04 20:22:39
FWIW, in general the corresponding file for src/fo
braveyao
2016/05/18 00:41:46
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.media; |
| + |
| +import android.annotation.TargetApi; |
| +import android.app.Activity; |
| +import android.app.Fragment; |
| +import android.app.FragmentManager; |
| +import android.app.FragmentTransaction; |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.graphics.PixelFormat; |
| +import android.hardware.display.DisplayManager; |
| +import android.hardware.display.VirtualDisplay; |
| +import android.media.Image; |
| +import android.media.ImageReader; |
| +import android.media.projection.MediaProjection; |
| +import android.media.projection.MediaProjectionManager; |
| +import android.os.Build; |
| +import android.os.Handler; |
| +import android.os.HandlerThread; |
| +import android.util.DisplayMetrics; |
| +import android.view.Display; |
| +import android.view.Surface; |
| + |
| +import org.chromium.base.ApplicationStatus; |
| +import org.chromium.base.Log; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| + |
| +import java.nio.ByteBuffer; |
| + |
| +/** |
| + * This class implements Screen Capture using projection API, introduced in Android |
| + * API 21 (L Release). Capture takes place in the current Looper, while pixel |
| + * download takes place in another thread used by ImageReader. |
| + **/ |
| +@JNINamespace("media") |
| +@TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| +public class ScreenCapture extends Fragment { |
| + // Internal class implementing the ImageReader listener. Gets pinged when a |
| + // new frame is been captured and downloaded to memory-backed buffers. |
| + private class CrImageReaderListener implements ImageReader.OnImageAvailableListener { |
|
mcasas
2016/04/27 01:19:26
This is _very_ similar class to [1] except for the
braveyao
2016/05/04 18:49:40
As we discussed offline. I understand the concern
mcasas
2016/05/04 19:56:47
It's not complicated. You're probably confused as
braveyao
2016/05/18 00:41:46
Tried a bit and saw your point. Added a TODO for i
|
| + @Override |
| + public void onImageAvailable(ImageReader reader) { |
| + synchronized (mCaptureStateLock) { |
| + if (mCaptureState != CaptureState.STARTED) { |
| + Log.e(TAG, "Get captured frame in unexpected state."); |
| + return; |
| + } |
| + } |
| + |
| + Image image = null; |
| + try { |
| + image = reader.acquireLatestImage(); |
| + if (image == null) return; |
| + if (image.getFormat() != PixelFormat.RGBA_8888) { |
| + Log.e(TAG, "Unexpected image format: " + image.getFormat() + " or #planes: " |
| + + image.getPlanes().length); |
| + return; |
| + } |
| + |
| + Image.Plane[] planes = image.getPlanes(); |
| + ByteBuffer buffer = (ByteBuffer) planes[0].getBuffer(); |
| + buffer.get(mCapturedData); |
| + |
| + nativeOnFrameAvailable(mNativeScreenCaptureMachineAndroid, mCapturedData, |
| + image.getCropRect().width(), image.getCropRect().height(), |
| + image.getTimestamp()); |
| + } catch (IllegalStateException ex) { |
| + Log.e(TAG, "acquireLatestImage():" + ex); |
| + return; |
| + } finally { |
| + if (image != null) image.close(); |
| + } |
| + } |
| + } |
| + |
| + private class MediaProjectionCallback extends MediaProjection.Callback { |
| + @Override |
| + public void onStop() { |
| + changeCaptureStateAndNotify(CaptureState.STOPPED); |
| + mMediaProjection = null; |
| + if (mVirtualDisplay == null) return; |
| + mVirtualDisplay.release(); |
| + mVirtualDisplay = null; |
| + } |
| + } |
| + |
| + private byte[] mCapturedData; |
| + private final Context mContext; |
| + |
| + private MediaProjection mMediaProjection; |
| + private MediaProjectionManager mMediaProjectionManager; |
| + private VirtualDisplay mVirtualDisplay; |
| + private static final int REQUEST_MEDIA_PROJECTION = 1; |
| + |
| + private Surface mSurface; |
| + private ImageReader mImageReader = null; |
| + private int mScreenDensity; |
| + private int mWidth; |
| + private int mHeight; |
| + private int mResultCode; |
| + private Intent mResultData; |
| + |
| + // Native callback context variable. |
| + private final long mNativeScreenCaptureMachineAndroid; |
| + |
| + private static enum CaptureState { ATTACHED, ALLOWED, STARTED, STOPPING, STOPPED } |
| + private CaptureState mCaptureState = CaptureState.STOPPED; |
| + private final Object mCaptureStateLock = new Object(); |
| + |
| + private static final String TAG = "ScreenCaptureMachine"; |
| + |
| + ScreenCapture(Context context, long nativeScreenCaptureMachineAndroid) { |
| + mContext = context; |
| + mNativeScreenCaptureMachineAndroid = nativeScreenCaptureMachineAndroid; |
| + try { |
| + Activity activity = ApplicationStatus.getLastTrackedFocusedActivity(); |
| + if (activity == null) { |
| + Log.e(TAG, "activity is null"); |
| + return; |
| + } |
| + |
| + FragmentManager fragmentManager = activity.getFragmentManager(); |
| + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); |
| + fragmentTransaction.add(this, "screencapture"); |
| + fragmentTransaction.commit(); |
| + |
| + DisplayMetrics metrics = new DisplayMetrics(); |
| + Display display = activity.getWindowManager().getDefaultDisplay(); |
| + display.getMetrics(metrics); |
| + mScreenDensity = metrics.densityDpi; |
| + } catch (Exception e) { |
|
mcasas
2016/04/27 01:19:26
No-no, don't catch generic exceptions out of a
la
braveyao
2016/05/04 18:49:40
Done.
mcasas
2016/05/04 19:56:47
Still you need to not catch Exception, but
each in
braveyao
2016/05/18 00:41:46
I'm aware of this. But as to the methods here, the
|
| + Log.e(TAG, "ScreenCaptureExcaption " + e); |
| + } |
| + } |
| + |
| + @Override |
| + public void onAttach(Context context) { |
| + super.onAttach(context); |
| + Log.d(TAG, "onAttach"); |
| + changeCaptureStateAndNotify(CaptureState.ATTACHED); |
| + } |
| + |
| + // This method was deprecated in API level 23 by onAttach(Context). |
|
mcasas
2016/04/27 01:19:26
// TODO(braveyao)
make a crbug.com for it and link
braveyao
2016/05/04 18:49:40
Done.
Not add a crbug though, since this will exis
|
| + @SuppressWarnings("deprecation") |
| + @Override |
| + public void onAttach(Activity activity) { |
| + super.onAttach(activity); |
| + Log.d(TAG, "onAttach"); |
| + changeCaptureStateAndNotify(CaptureState.ATTACHED); |
| + } |
| + |
| + @Override |
| + public void onDetach() { |
| + super.onDetach(); |
| + Log.d(TAG, "onDetach"); |
| + stopCapture(); |
| + } |
| + |
| + @CalledByNative |
| + public boolean startPrompt(int width, int height) { |
| + Log.d(TAG, "startPrompt"); |
| + synchronized (mCaptureStateLock) { |
| + while (mCaptureState != CaptureState.ATTACHED) { |
| + try { |
| + mCaptureStateLock.wait(); |
| + } catch (InterruptedException ex) { |
| + Log.e(TAG, "ScreenCaptureEvent: " + ex); |
|
mcasas
2016/04/27 01:19:26
It's not an event, is an exception.
braveyao
2016/05/04 18:49:40
Done.
|
| + } |
| + } |
| + } |
| + |
| + try { |
| + mWidth = width; |
| + mHeight = height; |
| + int expectedFrameSize = mWidth * mHeight * 4; |
| + mCapturedData = new byte[expectedFrameSize]; |
| + |
| + mMediaProjectionManager = (MediaProjectionManager) mContext.getSystemService( |
| + Context.MEDIA_PROJECTION_SERVICE); |
| + if (mMediaProjectionManager == null) { |
| + Log.e(TAG, "mMediaProjectionManager is null"); |
| + return false; |
| + } |
| + |
| + startActivityForResult( |
| + mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION); |
| + } catch (Exception e) { |
| + Log.e(TAG, "ScreenCaptureExcaption " + e); |
|
mcasas
2016/04/27 01:19:26
typo
braveyao
2016/05/04 18:49:40
Done.
|
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + @Override |
| + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| + if (requestCode == REQUEST_MEDIA_PROJECTION) { |
| + int result; |
| + if (resultCode != Activity.RESULT_OK) { |
| + result = 0; |
| + } else { |
| + result = 1; |
|
mcasas
2016/04/27 01:19:26
Maybe |result| should be boolean?
braveyao
2016/05/04 18:49:40
Done.
|
| + mResultCode = resultCode; |
| + mResultData = data; |
| + changeCaptureStateAndNotify(CaptureState.ALLOWED); |
| + } |
| + nativeOnActivityResult(mNativeScreenCaptureMachineAndroid, result); |
| + } |
| + } |
| + |
| + // start screen capture. |
|
mcasas
2016/04/27 01:19:26
s/start/Starts/
But, honestly, this comment and l.
braveyao
2016/05/04 18:49:40
Done.
|
| + @CalledByNative |
| + public void startCapture() { |
| + Log.d(TAG, "startCapture"); |
| + synchronized (mCaptureStateLock) { |
| + if (mCaptureState != CaptureState.ALLOWED) { |
| + Log.e(TAG, "startCapture() invoked without user permission."); |
| + return; |
| + } |
| + } |
| + mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData); |
| + if (mMediaProjection == null) { |
| + Log.d(TAG, "mMediaProjection is null"); |
|
mcasas
2016/04/27 01:19:26
Log.e
braveyao
2016/05/04 18:49:40
Done.
|
| + return; |
| + } |
| + mMediaProjection.registerCallback(new MediaProjectionCallback(), null); |
| + |
| + final int maxImages = 2; |
| + mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, maxImages); |
| + mSurface = mImageReader.getSurface(); |
| + HandlerThread thread = new HandlerThread("Screen"); |
| + thread.start(); |
| + final Handler backgroundHandler = new Handler(thread.getLooper()); |
| + final CrImageReaderListener imageReaderListener = new CrImageReaderListener(); |
| + mImageReader.setOnImageAvailableListener(imageReaderListener, backgroundHandler); |
| + |
| + mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture", mWidth, mHeight, |
| + mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mSurface, null, |
| + null); |
| + |
| + changeCaptureStateAndNotify(CaptureState.STARTED); |
| + } |
| + |
| + // Stops screen capture. |
| + @CalledByNative |
| + public void stopCapture() { |
| + Log.d(TAG, "stopCapture"); |
| + synchronized (mCaptureStateLock) { |
| + if (mMediaProjection != null && mCaptureState == CaptureState.STARTED) { |
| + mMediaProjection.stop(); |
| + changeCaptureStateAndNotify(CaptureState.STOPPING); |
| + } |
| + |
| + while (mCaptureState != CaptureState.STOPPED) { |
| + try { |
| + mCaptureStateLock.wait(); |
| + } catch (InterruptedException ex) { |
| + Log.e(TAG, "ScreenCaptureEvent: " + ex); |
| + } |
| + } |
| + } |
| + } |
| + |
| + private void changeCaptureStateAndNotify(CaptureState state) { |
| + synchronized (mCaptureStateLock) { |
| + mCaptureState = state; |
| + mCaptureStateLock.notifyAll(); |
| + } |
| + } |
| + |
| + // Method for ScreenCapture implementations to call back native code. |
| + private native void nativeOnFrameAvailable(long nativeScreenCaptureMachineAndroid, byte[] data, |
| + int cropWidth, int cropHeight, long timestamp); |
| + |
| + // Method for ScreenCapture implementations to call back native code. |
| + private native void nativeOnActivityResult(long nativeScreenCaptureMachineAndroid, int result); |
| +} |