-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from ChillingVan/dev
Dev
- Loading branch information
Showing
14 changed files
with
326 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...ample/src/main/java/com/chillingvan/canvasglsample/screenRecord/ScreenRecordActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.chillingvan.canvasglsample.screenRecord; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.graphics.SurfaceTexture; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.annotation.RequiresApi; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.chillingvan.canvasgl.glcanvas.RawTexture; | ||
import com.chillingvan.canvasgl.glview.texture.GLMultiTexProducerView; | ||
import com.chillingvan.canvasgl.glview.texture.GLTexture; | ||
import com.chillingvan.canvasglsample.R; | ||
|
||
import java.util.List; | ||
|
||
import static com.chillingvan.canvasglsample.screenRecord.ScreenRecordHelper.REQUEST_MEDIA_PROJECTION; | ||
|
||
/** | ||
* Created by Chilling on 2020/3/7. | ||
*/ | ||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
public class ScreenRecordActivity extends AppCompatActivity { | ||
|
||
|
||
private ScreenRecordTextureView mScreenRecordTextureView; | ||
private TextView mToggleBtn; | ||
private ScreenRecordHelper mScreenRecordHelper = new ScreenRecordHelper(); | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_screen_record); | ||
mScreenRecordTextureView = findViewById(R.id.texture_screen_record); | ||
mToggleBtn = findViewById(R.id.btn_start_screen_record); | ||
|
||
initTextureView(); | ||
mToggleBtn.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
if (mScreenRecordHelper.isRecording()) { | ||
mToggleBtn.setText("Start"); | ||
mScreenRecordHelper.stopScreenCapture(); | ||
} else { | ||
mToggleBtn.setText("Stop"); | ||
mScreenRecordHelper.start(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void initTextureView() { | ||
mScreenRecordTextureView.setSurfaceTextureCreatedListener(new GLMultiTexProducerView.SurfaceTextureCreatedListener() { | ||
@Override | ||
public void onCreated(List<GLTexture> producedTextureList) { | ||
GLTexture texture = producedTextureList.get(0); | ||
SurfaceTexture surfaceTexture = texture.getSurfaceTexture(); | ||
RawTexture rawTexture = texture.getRawTexture(); | ||
|
||
// SurfaceTexture need to call this for screen record | ||
surfaceTexture.setDefaultBufferSize(rawTexture.getWidth(), rawTexture.getHeight()); | ||
surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() { | ||
@Override | ||
public void onFrameAvailable(SurfaceTexture surfaceTexture) { | ||
mScreenRecordTextureView.requestRender(); | ||
} | ||
}); | ||
mScreenRecordHelper.init(ScreenRecordActivity.this, texture); | ||
} | ||
}); | ||
} | ||
|
||
|
||
@Override | ||
public void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
if (requestCode == REQUEST_MEDIA_PROJECTION) { | ||
if (resultCode != Activity.RESULT_OK) { | ||
Toast.makeText(this, "User Canceled", Toast.LENGTH_SHORT).show(); | ||
return; | ||
} | ||
mScreenRecordHelper.fetchPermissionResultCode(resultCode, data); | ||
mScreenRecordHelper.start(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
mScreenRecordHelper.destroy(); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...lsample/src/main/java/com/chillingvan/canvasglsample/screenRecord/ScreenRecordHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.chillingvan.canvasglsample.screenRecord; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.hardware.display.DisplayManager; | ||
import android.hardware.display.VirtualDisplay; | ||
import android.media.projection.MediaProjection; | ||
import android.media.projection.MediaProjectionManager; | ||
import android.os.Build; | ||
import android.support.annotation.RequiresApi; | ||
import android.util.DisplayMetrics; | ||
import android.view.Surface; | ||
|
||
import com.chillingvan.canvasgl.glcanvas.RawTexture; | ||
import com.chillingvan.canvasgl.glview.texture.GLTexture; | ||
|
||
/** | ||
* Created by Chilling on 2020/3/7. | ||
*/ | ||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
public class ScreenRecordHelper { | ||
|
||
public static final int REQUEST_MEDIA_PROJECTION = 1; | ||
|
||
private VirtualDisplay mVirtualDisplay; | ||
private MediaProjectionManager mMediaProjectionManager; | ||
private MediaProjection mMediaProjection; | ||
private RawTexture mRawTexture; | ||
private Surface mSurface; | ||
private Activity mActivity; | ||
private int mScreenDensity; | ||
private int mActivityResultCode; | ||
private Intent mActivityResultData; | ||
|
||
public void init(Activity activity, GLTexture glTexture) { | ||
mActivity = activity; | ||
mSurface = new Surface(glTexture.getSurfaceTexture()); | ||
mRawTexture = glTexture.getRawTexture(); | ||
mMediaProjectionManager = (MediaProjectionManager) activity. | ||
getSystemService(Context.MEDIA_PROJECTION_SERVICE); | ||
|
||
DisplayMetrics metrics = new DisplayMetrics(); | ||
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); | ||
mScreenDensity = metrics.densityDpi; | ||
} | ||
|
||
public void start() { | ||
if (mRawTexture == null) { | ||
return; | ||
} | ||
if (mMediaProjection != null) { | ||
setupVirtualDisplay(); | ||
} else if (mActivityResultCode != 0 && mActivityResultData != null) { | ||
setUpMediaProjection(); | ||
setupVirtualDisplay(); | ||
} else { | ||
startRequestPermission(); | ||
} | ||
} | ||
|
||
private void startRequestPermission() { | ||
mActivity.startActivityForResult( | ||
mMediaProjectionManager.createScreenCaptureIntent(), | ||
REQUEST_MEDIA_PROJECTION); | ||
} | ||
|
||
public void fetchPermissionResultCode(int resultCode, Intent data) { | ||
mActivityResultCode = resultCode; | ||
mActivityResultData = data; | ||
} | ||
|
||
private void setUpMediaProjection() { | ||
mMediaProjection = mMediaProjectionManager.getMediaProjection(mActivityResultCode, mActivityResultData); | ||
} | ||
|
||
private void setupVirtualDisplay() { | ||
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture", | ||
mRawTexture.getWidth(), mRawTexture.getHeight(), mScreenDensity, | ||
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, | ||
mSurface, null, null); | ||
} | ||
|
||
public void stopScreenCapture() { | ||
if (mVirtualDisplay == null) { | ||
return; | ||
} | ||
mVirtualDisplay.release(); | ||
mVirtualDisplay = null; | ||
} | ||
|
||
public void destroy() { | ||
if (mMediaProjection != null) { | ||
mMediaProjection.stop(); | ||
mMediaProjection = null; | ||
} | ||
} | ||
|
||
public boolean isRecording() { | ||
return mVirtualDisplay != null; | ||
} | ||
} |
Oops, something went wrong.