Advertisement
Guest User

screencast

a guest
Aug 29th, 2015
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.37 KB | None | 0 0
  1. package com.example.stw.myapplication;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.hardware.display.DisplayManager;
  7. import android.media.MediaCodec;
  8. import android.media.MediaCodecInfo;
  9. import android.media.MediaFormat;
  10. import android.media.MediaMuxer;
  11. import android.media.projection.MediaProjection;
  12. import android.media.projection.MediaProjectionManager;
  13. import android.os.Bundle;
  14. import android.os.Handler;
  15. import android.os.Looper;
  16. import android.util.DisplayMetrics;
  17. import android.view.Display;
  18. import android.view.Menu;
  19. import android.view.MenuItem;
  20. import android.view.Surface;
  21. import android.view.SurfaceView;
  22. import android.view.View;
  23. import android.widget.Button;
  24. import android.widget.TextView;
  25.  
  26. import java.io.IOException;
  27. import java.nio.ByteBuffer;
  28.  
  29. public class MainActivity extends Activity {
  30.     private boolean recording=false;
  31.     private Button button;
  32.     private MediaProjectionManager mMediaProjectionManager;
  33.     private static final int REQUEST_CODE_CAPTURE_PERM = 1234;
  34.     private MediaProjection mMediaProjection;
  35.     private static final String VIDEO_MIME_TYPE = "video/avc";
  36.     private static final int VIDEO_WIDTH = 1280;
  37.     private static final int VIDEO_HEIGHT = 720;
  38.     private DisplayMetrics metrics;
  39.     private int screenDensity;
  40.     private int screenWidth;
  41.     private int screenHeight;
  42.     // …
  43.     private boolean mMuxerStarted = false;
  44.     private Surface mInputSurface;
  45.     private MediaMuxer mMuxer;
  46.     private MediaCodec mVideoEncoder;
  47.     private MediaCodec.BufferInfo mVideoBufferInfo;
  48.     private int mTrackIndex = -1;
  49.     private SurfaceView mSurfaceView;
  50.  
  51.     @Override
  52.     protected void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);
  54.         setContentView(R.layout.activity_main);
  55.         mMediaProjectionManager = (MediaProjectionManager) getSystemService(
  56.                 android.content.Context.MEDIA_PROJECTION_SERVICE);
  57.     }
  58.  
  59.     @Override
  60.     public boolean onCreateOptionsMenu(Menu menu) {
  61.         // Inflate the menu; this adds items to the action bar if it is present.
  62.         getMenuInflater().inflate(R.menu.menu_main, menu);
  63.         return true;
  64.     }
  65.  
  66.     public void buttonOnClick(View view){
  67.         button=(Button) view;
  68.         if(recording){
  69.             recording=false;
  70.             ((Button) view).setText("Start Recording");
  71.             releaseEncoders();
  72.         }
  73.         else {
  74.             recording=true;
  75.             ((Button) view).setText("Stop Recording");
  76.             Intent permissionIntent = mMediaProjectionManager.createScreenCaptureIntent();
  77.             startActivityForResult(permissionIntent, REQUEST_CODE_CAPTURE_PERM);
  78.         }
  79.     }
  80.     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  81.         if (REQUEST_CODE_CAPTURE_PERM == requestCode) {
  82.             if (resultCode == RESULT_OK) {
  83.                 mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, intent);
  84.                 startRecording();
  85.             } else {
  86.             }
  87.         }
  88.     }
  89.     private final Handler mDrainHandler = new Handler(Looper.getMainLooper());
  90.     private Runnable mDrainEncoderRunnable = new Runnable() {
  91.         @Override
  92.         public void run() {
  93.             drainEncoder();
  94.         }
  95.     };
  96. // …
  97.  
  98.     private void startRecording() {
  99.         DisplayManager dm = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
  100.         Display defaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
  101.         if (defaultDisplay == null) {
  102.             throw new RuntimeException("No display found.");
  103.         }
  104.         prepareVideoEncoder();
  105.  
  106.         try {
  107.             mMuxer = new MediaMuxer(getFilesDir()+"/video7.mp4", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
  108.         } catch (IOException ioe) {
  109.             throw new RuntimeException("MediaMuxer creation failed", ioe);
  110.         }
  111.  
  112.         // Get the display size and density.
  113.         metrics = getResources().getDisplayMetrics();
  114.         screenDensity=metrics.densityDpi;
  115.         screenHeight=metrics.heightPixels;
  116.         screenWidth=metrics.widthPixels;
  117.  
  118.         // Start the video input.
  119.         mMediaProjection.createVirtualDisplay("Recording Display", screenWidth,
  120.                 screenHeight, screenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION, mInputSurface,
  121.                 null /* callback */, null /* handler */);
  122.  
  123.         // Start the encoders
  124.         drainEncoder();
  125.     }
  126.  
  127.     private void prepareVideoEncoder() {
  128.         mVideoBufferInfo = new MediaCodec.BufferInfo();
  129.         MediaFormat format = MediaFormat.createVideoFormat(VIDEO_MIME_TYPE, VIDEO_WIDTH, VIDEO_HEIGHT);
  130.         int frameRate = 30; // 30 fps
  131.  
  132.         // Set some required properties. The media codec may fail if these aren't defined.
  133.         format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
  134.                 MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
  135.         format.setInteger(MediaFormat.KEY_BIT_RATE, 6000000); // 6Mbps
  136.         format.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
  137.         format.setInteger(MediaFormat.KEY_CAPTURE_RATE, frameRate);
  138.         format.setInteger(MediaFormat.KEY_REPEAT_PREVIOUS_FRAME_AFTER, 1000000 / frameRate);
  139.         format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
  140.         format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1); // 1 seconds between I-frames
  141.  
  142.         // Create a MediaCodec encoder and configure it. Get a Surface we can use for recording into.
  143.         try {
  144.             mVideoEncoder = MediaCodec.createEncoderByType(VIDEO_MIME_TYPE);
  145.             mVideoEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  146.             mInputSurface = mVideoEncoder.createInputSurface();
  147.             mVideoEncoder.start();
  148.         } catch (IOException e) {
  149.             releaseEncoders();
  150.         }
  151.     }
  152.  
  153.     private boolean drainEncoder() {
  154.         mDrainHandler.removeCallbacks(mDrainEncoderRunnable);
  155.         while (true) {
  156.             int bufferIndex = mVideoEncoder.dequeueOutputBuffer(mVideoBufferInfo, 0);
  157.             if (bufferIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
  158.                 // nothing available yet
  159.                 break;
  160.             } else if (bufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
  161.                 // should happen before receiving buffers, and should only happen once
  162.                 if (mTrackIndex >= 0) {
  163.                     throw new RuntimeException("format changed twice");
  164.                 }
  165.                 mTrackIndex = mMuxer.addTrack(mVideoEncoder.getOutputFormat());
  166.                 if (!mMuxerStarted && mTrackIndex >= 0) {
  167.                     mMuxer.start();
  168.                     mMuxerStarted = true;
  169.                 }
  170.             } else if (bufferIndex < 0) {
  171.                 // not sure what's going on, ignore it
  172.             } else {
  173.                 ByteBuffer encodedData = mVideoEncoder.getOutputBuffer(bufferIndex);
  174.                 if (encodedData == null) {
  175.                     throw new RuntimeException("couldn't fetch buffer at index " + bufferIndex);
  176.                 }
  177.  
  178.                 if ((mVideoBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
  179.                     mVideoBufferInfo.size = 0;
  180.                 }
  181.  
  182.                 if (mVideoBufferInfo.size != 0) {
  183.                     if (mMuxerStarted) {
  184.                         encodedData.position(mVideoBufferInfo.offset);
  185.                         encodedData.limit(mVideoBufferInfo.offset + mVideoBufferInfo.size);
  186.                         mMuxer.writeSampleData(mTrackIndex, encodedData, mVideoBufferInfo);
  187.                     } else {
  188.                         // muxer not started
  189.                     }
  190.                 }
  191.  
  192.                 mVideoEncoder.releaseOutputBuffer(bufferIndex, false);
  193.  
  194.                 if ((mVideoBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
  195.                     break;
  196.                 }
  197.             }
  198.         }
  199.  
  200.         mDrainHandler.postDelayed(mDrainEncoderRunnable, 10);
  201.         return false;
  202.     }
  203.  
  204.     private void releaseEncoders() {
  205.         mDrainHandler.removeCallbacks(mDrainEncoderRunnable);
  206.         if (mMuxer != null) {
  207.             if (mMuxerStarted) {
  208.                 mMuxer.stop();
  209.             }
  210.             mMuxer.release();
  211.             mMuxer = null;
  212.             mMuxerStarted = false;
  213.         }
  214.         if (mVideoEncoder != null) {
  215.             mVideoEncoder.stop();
  216.             mVideoEncoder.release();
  217.             mVideoEncoder = null;
  218.         }
  219.         if (mInputSurface != null) {
  220.             mInputSurface.release();
  221.             mInputSurface = null;
  222.         }
  223.         if (mMediaProjection != null) {
  224.             mMediaProjection.stop();
  225.             mMediaProjection = null;
  226.         }
  227.         mVideoBufferInfo = null;
  228.         mDrainEncoderRunnable = null;
  229.         mTrackIndex = -1;
  230.     }
  231.  
  232.     @Override
  233.     public boolean onOptionsItemSelected(MenuItem item) {
  234.         // Handle action bar item clicks here. The action bar will
  235.         // automatically handle clicks on the Home/Up button, so long
  236.         // as you specify a parent activity in AndroidManifest.xml.
  237.         int id = item.getItemId();
  238.  
  239.         //noinspection SimplifiableIfStatement
  240.         if (id == R.id.action_settings) {
  241.             return true;
  242.         }
  243.  
  244.         return super.onOptionsItemSelected(item);
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement