Expose functionality to convert TextureBuffer to I420.

Bug: webrtc:8392
Change-Id: I79682efbef3aecbba904aa5047b229833fae25e8
Reviewed-on: https://webrtc-review.googlesource.com/8940
Commit-Queue: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20313}
diff --git a/sdk/android/api/org/webrtc/SurfaceTextureHelper.java b/sdk/android/api/org/webrtc/SurfaceTextureHelper.java
index 1fbc53f..8e26085 100644
--- a/sdk/android/api/org/webrtc/SurfaceTextureHelper.java
+++ b/sdk/android/api/org/webrtc/SurfaceTextureHelper.java
@@ -231,8 +231,11 @@
     });
   }
 
-  public void textureToYUV(final ByteBuffer buf, final int width, final int height,
-      final int stride, final int textureId, final float[] transformMatrix) {
+  /** Deprecated, use textureToYuv. */
+  @Deprecated
+  @SuppressWarnings("deprecation") // yuvConverter.convert is deprecated
+  void textureToYUV(final ByteBuffer buf, final int width, final int height, final int stride,
+      final int textureId, final float[] transformMatrix) {
     if (textureId != oesTextureId) {
       throw new IllegalStateException("textureToByteBuffer called with unexpected textureId");
     }
@@ -248,6 +251,25 @@
     });
   }
 
+  /**
+   * Posts to the correct thread to convert |textureBuffer| to I420. Must only be called with
+   * textures generated by this SurfaceTextureHelper.
+   */
+  public VideoFrame.I420Buffer textureToYuv(final TextureBuffer textureBuffer) {
+    if (textureBuffer.getTextureId() != oesTextureId) {
+      throw new IllegalStateException("textureToByteBuffer called with unexpected textureId");
+    }
+
+    final VideoFrame.I420Buffer[] result = new VideoFrame.I420Buffer[1];
+    ThreadUtils.invokeAtFrontUninterruptibly(handler, () -> {
+      if (yuvConverter == null) {
+        yuvConverter = new YuvConverter();
+      }
+      result[0] = yuvConverter.convert(textureBuffer);
+    });
+    return result[0];
+  }
+
   private void updateTexImage() {
     // SurfaceTexture.updateTexImage apparently can compete and deadlock with eglSwapBuffers,
     // as observed on Nexus 5. Therefore, synchronize it with the EGL functions.