title: Custom byteArray data to WebRTC videoTrack
body: <p>I need to use <a href="https://github.com/njovy/AppRTCDemo" rel="noreferrer">WebRTC</a> for android to send specific cropped(face) video to the videoChannel. I was able manipulate <a href="https://wing1.cs.ucla.edu/gitlab/tan/webrtc/blob/3d7a5007a72134ca8c066d9a3be52ef052e75b85/webrtc/api/android/java/src/org/webrtc/Camera1Session.java" rel="noreferrer">Camera1Session</a> class of WebRTC to get the face cropped. Right now I am setting it to an ImageView.
  <code>listenForBytebufferFrames()</code> of <code>Camera1Session.java</code></p>
  
  <pre><code>private void listenForBytebufferFrames() {
      this.camera.setPreviewCallbackWithBuffer(new PreviewCallback() {
          public void onPreviewFrame(byte[] data, Camera callbackCamera) {
              Camera1Session.this.checkIsOnCameraThread();
              if(callbackCamera != Camera1Session.this.camera) {
                  Logging.e("Camera1Session", "Callback from a different camera. This should never happen.");
              } else if(Camera1Session.this.state != Camera1Session.SessionState.RUNNING) {
                  Logging.d("Camera1Session", "Bytebuffer frame captured but camera is no longer running.");
              } else {
                  mFrameProcessor.setNextFrame(data, callbackCamera);
                  long captureTimeNs = TimeUnit.MILLISECONDS.toNanos(SystemClock.elapsedRealtime());
                  if(!Camera1Session.this.firstFrameReported) {
                      int startTimeMs = (int)TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - Camera1Session.this.constructionTimeNs);
                      Camera1Session.camera1StartTimeMsHistogram.addSample(startTimeMs);
                      Camera1Session.this.firstFrameReported = true;
                  }
  
                  ByteBuffer byteBuffer1 = ByteBuffer.wrap(data);
                  Frame outputFrame = new Frame.Builder()
                          .setImageData(byteBuffer1,
                                  Camera1Session.this.captureFormat.width,
                                  Camera1Session.this.captureFormat.height,
                                  ImageFormat.NV21)
                          .setTimestampMillis(mFrameProcessor.mPendingTimeMillis)
                          .setId(mFrameProcessor.mPendingFrameId)
                          .setRotation(3)
                          .build();
                  int w = outputFrame.getMetadata().getWidth();
                  int h = outputFrame.getMetadata().getHeight();
                  SparseArray&lt;Face&gt; detectedFaces = mDetector.detect(outputFrame);
                  if (detectedFaces.size() &gt; 0) {
  
                      Face face = detectedFaces.valueAt(0);
                      ByteBuffer byteBufferRaw = outputFrame.getGrayscaleImageData();
                      byte[] byteBuffer = byteBufferRaw.array();
                      YuvImage yuvimage  = new YuvImage(byteBuffer, ImageFormat.NV21, w, h, null);
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
                      //My crop logic to get face co-ordinates
  
                      yuvimage.compressToJpeg(new Rect(left, top, right, bottom), 80, baos);
                      final byte[] jpegArray = baos.toByteArray();
                      Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
  
                      Activity currentActivity = getActivity();
                      if (currentActivity instanceof CallActivity) {
                          ((CallActivity) currentActivity).setBitmapToImageView(bitmap); //face on ImageView is set just fine
                      }
                      Camera1Session.this.events.onByteBufferFrameCaptured(Camera1Session.this, data, Camera1Session.this.captureFormat.width, Camera1Session.this.captureFormat.height, Camera1Session.this.getFrameOrientation(), captureTimeNs);
                      Camera1Session.this.camera.addCallbackBuffer(data);
                  } else {
                      Camera1Session.this.events.onByteBufferFrameCaptured(Camera1Session.this, data, Camera1Session.this.captureFormat.width, Camera1Session.this.captureFormat.height, Camera1Session.this.getFrameOrientation(), captureTimeNs);
                      Camera1Session.this.camera.addCallbackBuffer(data);
                  }
  
              }
          }
      });
  }
  </code></pre>
  
  <p><code>jpegArray</code> is the final byteArray that I need to stream via <code>WebRTC</code>, which I tried with something like this:</p>
  
  <pre><code>Camera1Session.this.events.onByteBufferFrameCaptured(Camera1Session.this, jpegArray, (int) face.getWidth(), (int) face.getHeight(), Camera1Session.this.getFrameOrientation(), captureTimeNs);
  Camera1Session.this.camera.addCallbackBuffer(jpegArray);
  </code></pre>
  
  <p>Setting them up like this gives me following error:</p>
  
  <pre><code>../../webrtc/sdk/android/src/jni/androidvideotracksource.cc line 82
  Check failed: length &gt;= width * height + 2 * uv_width * ((height + 1) / 2) (2630 vs. 460800)
  </code></pre>
  
  <p>Which I assume is because <code>androidvideotracksource</code> does not get the same length of <code>byteArray</code> that it expects, since the frame is cropped now.
  Could someone point me in the direction of how to achieve it? Is this the correct way/place to manipulate the data and feed into the <code>videoTrack</code>?</p>
  
  <p><strong>Edit:</strong><code>bitmap</code> of byteArray <code>data</code> does not give me a camera preview on <code>ImageView</code>, unlike byteArray <code>jpegArray</code>. Maybe because they are packed differently?</p>