| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import android.graphics.Matrix; |
| 14 | import android.opengl.GLES11Ext; |
| 15 | import android.opengl.GLES20; |
| 16 | import java.nio.ByteBuffer; |
| 17 |
| 18 | /** |
| 19 | * Java version of webrtc::VideoFrame and webrtc::VideoFrameBuffer. A difference from the C++ |
| 20 | * version is that no explicit tag is used, and clients are expected to use 'instanceof' to find the |
| 21 | * right subclass of the buffer. This allows clients to create custom VideoFrame.Buffer in |
| 22 | * arbitrary format in their custom VideoSources, and then cast it back to the correct subclass in |
| 23 | * their custom VideoSinks. All implementations must also implement the toI420() function, |
| 24 | * converting from the underlying representation if necessary. I420 is the most widely accepted |
| 25 | * format and serves as a fallback for video sinks that can only handle I420, e.g. the internal |
| 26 | * WebRTC software encoders. |
| 27 | */ |
| 28 | public class VideoFrame implements RefCounted { |
| 29 | /** |
| 30 | * Implements image storage medium. Might be for example an OpenGL texture or a memory region |
| 31 | * containing I420-data. |
| 32 | * |
| 33 | * <p>Reference counting is needed since a video buffer can be shared between multiple VideoSinks, |
| 34 | * and the buffer needs to be returned to the VideoSource as soon as all references are gone. |
| 35 | */ |
| 36 | public interface Buffer extends RefCounted { |
| 37 | /** |
| 38 | * Representation of the underlying buffer. Currently, only NATIVE and I420 are supported. |
| 39 | */ |
| 40 | default int getBufferType() { |
| 41 | return 0; |
| 42 | } |
| 43 | /** |
| 44 | * Resolution of the buffer in pixels. |
| 45 | */ |
| 46 | @CalledByNative("Buffer") int getWidth(); |
| 47 | @CalledByNative("Buffer") int getHeight(); |
| 48 |
| 49 | /** |
| 50 | * Returns a memory-backed frame in I420 format. If the pixel data is in another format, a |
| 51 | * conversion will take place. All implementations must provide a fallback to I420 for |
| 52 | * compatibility with e.g. the internal WebRTC software encoders. |
| 53 | */ |
| 54 | @CalledByNative("Buffer") I420Buffer toI420(); |
| 55 |
| 56 | @Override @CalledByNative("Buffer") void retain(); |
| 57 | @Override @CalledByNative("Buffer") void release(); |
| 58 |
| 59 | /** |
| 60 | * Crops a region defined by |cropx|, |cropY|, |cropWidth| and |cropHeight|. Scales it to size |
| 61 | * |scaleWidth| x |scaleHeight|. |
| 62 | */ |
| 63 | @CalledByNative("Buffer") |
| 64 | Buffer cropAndScale( |
| 65 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight); |
| 66 | } |
| 67 |
| 68 | /** |
| 69 | * Interface for I420 buffers. |
| 70 | */ |
| 71 | public interface I420Buffer extends Buffer { |
| 72 | @Override |
| 73 | default int getBufferType() { |
| 74 | return 1; |
| 75 | } |
| 76 | /** |
| 77 | * Returns a direct ByteBuffer containing Y-plane data. The buffer capacity is at least |
| 78 | * getStrideY() * getHeight() bytes. The position of the returned buffer is ignored and must |
| 79 | * be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 80 | * implementations must return a new ByteBuffer or slice for each call. |
| 81 | */ |
| 82 | @CalledByNative("I420Buffer") ByteBuffer getDataY(); |
| 83 | /** |
| 84 | * Returns a direct ByteBuffer containing U-plane data. The buffer capacity is at least |
| 85 | * getStrideU() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored |
| 86 | * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 87 | * implementations must return a new ByteBuffer or slice for each call. |
| 88 | */ |
| 89 | @CalledByNative("I420Buffer") ByteBuffer getDataU(); |
| 90 | /** |
| 91 | * Returns a direct ByteBuffer containing V-plane data. The buffer capacity is at least |
| 92 | * getStrideV() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored |
| 93 | * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 94 | * implementations must return a new ByteBuffer or slice for each call. |
| 95 | */ |
| 96 | @CalledByNative("I420Buffer") ByteBuffer getDataV(); |
| 97 |
| 98 | @CalledByNative("I420Buffer") int getStrideY(); |
| 99 | @CalledByNative("I420Buffer") int getStrideU(); |
| 100 | @CalledByNative("I420Buffer") int getStrideV(); |
| 101 | } |
| 102 |
| 103 | /** |
| 104 | * Interface for buffers that are stored as a single texture, either in OES or RGB format. |
| 105 | */ |
| 106 | public interface TextureBuffer extends Buffer { |
| 107 | enum Type { |
| 108 | OES(GLES11Ext.GL_TEXTURE_EXTERNAL_OES), |
| 109 | RGB(GLES20.GL_TEXTURE_2D); |
| 110 |
| 111 | private final int glTarget; |
| 112 |
| 113 | private Type(final int glTarget) { |
| 114 | this.glTarget = glTarget; |
| 115 | } |
| 116 |
| 117 | public int getGlTarget() { |
| 118 | return glTarget; |
| 119 | } |
| 120 | } |
| 121 |
| 122 | Type getType(); |
| 123 | int getTextureId(); |
| 124 |
| 125 | /** |
| 126 | * Retrieve the transform matrix associated with the frame. This transform matrix maps 2D |
| 127 | * homogeneous coordinates of the form (s, t, 1) with s and t in the inclusive range [0, 1] to |
| 128 | * the coordinate that should be used to sample that location from the buffer. |
| 129 | */ |
| 130 | Matrix getTransformMatrix(); |
| 131 | } |
| 132 |
| 133 | private final Buffer buffer; |
| 134 | private final int rotation; |
| 135 | private final long timestampNs; |
| 136 |
| 137 | /** |
| 138 | * Constructs a new VideoFrame backed by the given {@code buffer}. |
| 139 | * |
| 140 | * @note Ownership of the buffer object is tranferred to the new VideoFrame. |
| 141 | */ |
| 142 | @CalledByNative |
| 143 | public VideoFrame(Buffer buffer, int rotation, long timestampNs) { |
| 144 | if (buffer == null) { |
| 145 | throw new IllegalArgumentException("buffer not allowed to be null"); |
| 146 | } |
| 147 | if (rotation % 90 != 0) { |
| 148 | throw new IllegalArgumentException("rotation must be a multiple of 90"); |
| 149 | } |
| 150 | this.buffer = buffer; |
| 151 | this.rotation = rotation; |
| 152 | this.timestampNs = timestampNs; |
| 153 | } |
| 154 |
| 155 | @CalledByNative |
| 156 | public Buffer getBuffer() { |
| 157 | return buffer; |
| 158 | } |
| 159 |
| 160 | /** |
| 161 | * Rotation of the frame in degrees. |
| 162 | */ |
| 163 | @CalledByNative |
| 164 | public int getRotation() { |
| 165 | return rotation; |
| 166 | } |
| 167 |
| 168 | /** |
| 169 | * Timestamp of the frame in nano seconds. |
| 170 | */ |
| 171 | @CalledByNative |
| 172 | public long getTimestampNs() { |
| 173 | return timestampNs; |
| 174 | } |
| 175 |
| 176 | public int getRotatedWidth() { |
| 177 | if (rotation % 180 == 0) { |
| 178 | return buffer.getWidth(); |
| 179 | } |
| 180 | return buffer.getHeight(); |
| 181 | } |
| 182 |
| 183 | public int getRotatedHeight() { |
| 184 | if (rotation % 180 == 0) { |
| 185 | return buffer.getHeight(); |
| 186 | } |
| 187 | return buffer.getWidth(); |
| 188 | } |
| 189 |
| 190 | @Override |
| 191 | public void retain() { |
| 192 | buffer.retain(); |
| 193 | } |
| 194 |
| 195 | @Override |
| 196 | @CalledByNative |
| 197 | public void release() { |
| 198 | buffer.release(); |
| 199 | } |
| 200 | } |
| 201 |