title: Screen Sharing between Devices using Media Projection API
body: <p>I am developing an app which has the functionality of sharing screens with other apps.</p>
  
  <p>I used the Media projection API for this. I also used MediaMuxer to combine the audio and video outputs for screen sharing.</p>
  
  <p>I know that Media Projection APIs are used for screen recording but all I want is to share the screen while recording.</p>
  
  <p>For this, I have modified the writeSampleData method of the MediaMuxer class to send bytes via a socket to the other device over the network.</p>
  
  <p>Below is the code for that:</p>
  
  <pre><code>OutputStream outStream;
  </code></pre>
  
  <p>outStream = ScreenRecordingActivity.getInstance().socket.getOutputStream();</p>
  
  <pre><code>void writeSampleData(final int trackIndex, final ByteBuffer byteBuf, final MediaCodec.BufferInfo bufferInfo) {
      if (mStatredCount &gt; 0) {
          mMediaMuxer.writeSampleData(trackIndex, byteBuf, bufferInfo);
  
          if (bufferInfo.size != 0) {
  
              byteBuf.position(bufferInfo.offset);
              byteBuf.limit(bufferInfo.offset + bufferInfo.size);
  
              if (outStream != null) {
  
                  try {
  
                      byte[] bytes = new byte[byteBuf.remaining()];
                      byteBuf.get(bytes);
  
                      //Send the data
                      outStream.write(bytes);
                      outStream.flush();
  
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The bytes are successfully transferred via socket and I am also able to receive these bytes at the receiver's end.</p>
  
  <p>Below is the code for receiving bytes at the receiver's end:</p>
  
  <pre><code>private class SocketThread implements Runnable {
      @Override
      public void run() {
  
          Socket socket;
          try {
              serverSocket = new ServerSocket(SERVER_PORT);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          if (null != serverSocket) {
              while (!Thread.currentThread().isInterrupted()) {
                  try {
                      socket = serverSocket.accept();
                      CommunicationThread commThread = new CommunicationThread(socket);
                      new Thread(commThread).start();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
  
  
      class CommunicationThread implements Runnable {
  
          InputStream in;
          DataInputStream dis;
  
          public CommunicationThread(Socket clientSocket) {
  
  
              updateMessage("Server Started...");
          }
  
          public void run() {           
  
              while (!Thread.currentThread().isInterrupted()) {
  
                  try {                       
  
                      byte[] data = new byte[512];               
  
                  } catch (Exception e) {                    
  
                      e.printStackTrace();
  
                      try {
                          fos.close();
                      } catch (Exception e1) {
  
                          e1.printStackTrace();
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I followed the following links for Screen Sharing:</p>
  
  <p><a href="https://github.com/googlesamples/android-ScreenCapture" rel="nofollow noreferrer">Screen capture</a></p>
  
  <p><a href="https://github.com/chinmoyp/screenrecorder" rel="nofollow noreferrer">screenrecorder</a></p>
  
  <p><a href="https://stackoverflow.com/questions/32513379/how-to-record-screen-and-take-screenshots-using-android-api">Screen recording with mediaProjection</a></p>
  
  <p>I used some code from the above examples to make an app.</p>
  
  <p>All I want to know is how to handle the bytes at the receiver. How do I  format these bytes to play a live stream from the sender's side?</p>
  
  <p>Am I following the correct approach for sending and receiving byte data?</p>
  
  <p>Does MediaProjection allow one to stream the Screen while recording between applications?</p>
  
  <p>Any help will be deeply appreciated.</p>