title: Stream android screen
body: <p>I am trying to fallow <a href="https://github.com/Truiton/ScreenCapture" rel="noreferrer">Truiton ScreenCapture</a> example, in order to record the device screen using <a href="https://developer.android.com/reference/android/media/projection/MediaProjection.html" rel="noreferrer">MediaProjection</a></p>
  
  <p>When saving the recording localy it works</p>
  
  <pre><code>    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
      mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  
      String localFilePath = getLocalFilePath();
      mMediaRecorder.setOutputFile(localFilePath);
  
      mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
      mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
      mMediaRecorder.setVideoFrameRate(30);
      mMediaRecorder.prepare();
  </code></pre>
  
  <p>How ever when changing to work with FileDescriptor it's not</p>
  
  <pre><code>    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
      mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  
      String hostname = "10.26.100.18";
      int port = 2007;
      Socket socket = new Socket(InetAddress.getByName(hostname), port);
      ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.fromSocket(socket);
      LocalServerSocket localServerSocket = new LocalServerSocket(fileDescriptor.getFileDescriptor());
  
      mMediaRecorder.setOutputFile(localServerSocket.getFileDescriptor());
  
      mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
      mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
      mMediaRecorder.setVideoFrameRate(30);
      mMediaRecorder.prepare();
  </code></pre>
  
  <p>If not using the LocalServerSocket, then <code>mMediaRecorder.prepare()</code> throw exception, now that I am using it, get exception in the below method in <code>mMediaRecorder.getSurface()</code></p>
  
  <pre><code>private VirtualDisplay createVirtualDisplay() {
      return mMediaProjection.createVirtualDisplay("MainActivity",
              DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
              DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
              mMediaRecorder.getSurface(), null /*Callbacks*/, null
              /*Handler*/);
  }
  </code></pre>
  
  <p>The exception</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: failed to get surface
     at android.media.MediaRecorder.getSurface(Native Method)
     at com.truiton.screencapture.MainActivity$override.createVirtualDisplay(MainActivity.java:172)
     at com.truiton.screencapture.MainActivity$override.onActivityResult(MainActivity.java:133)
     at com.truiton.screencapture.MainActivity$override.access$dispatch(MainActivity.java)
     at com.truiton.screencapture.MainActivity.onActivityResult(MainActivity.java:0)
     at android.app.Activity.dispatchActivityResult(Activity.java:6456)
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
     at android.app.ActivityThread.-wrap16(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  </code></pre>
  
  <p>This is my Java server, I get socket after <code>mMediaRecorder.prepare()</code> is called and it stuck on <code>inputStream.read</code> as eccpected. The exception in Android happands when I call <code>mMediaRecorder.start()</code></p>
  
  <pre><code>    final byte[] buffer = new byte[1024];
      try {
          ServerSocket serverSocket = new ServerSocket(2007);
          while (!serverSocket.isClosed()) {
              Socket socket = serverSocket.accept();
              File videoFile = createEmptyVideoFile();
              FileOutputStream outputStream = new FileOutputStream(videoFile);
              InputStream inputStream = socket.getInputStream();
              int length = inputStream.read(buffer);
              while (length != -1) {
                  outputStream.write(buffer, 0, length);
                  length = inputStream.read(buffer);
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>