magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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; |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 14 | import android.opengl.GLES11Ext; |
| 15 | import android.opengl.GLES20; |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 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 | */ |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 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 { |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Resolution of the buffer in pixels. |
| 39 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 40 | @CalledByNative("Buffer") int getWidth(); |
| 41 | @CalledByNative("Buffer") int getHeight(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Returns a memory-backed frame in I420 format. If the pixel data is in another format, a |
| 45 | * conversion will take place. All implementations must provide a fallback to I420 for |
| 46 | * compatibility with e.g. the internal WebRTC software encoders. |
| 47 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 48 | @CalledByNative("Buffer") I420Buffer toI420(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 49 | |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 50 | @Override @CalledByNative("Buffer") void retain(); |
| 51 | @Override @CalledByNative("Buffer") void release(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Crops a region defined by |cropx|, |cropY|, |cropWidth| and |cropHeight|. Scales it to size |
| 55 | * |scaleWidth| x |scaleHeight|. |
| 56 | */ |
Niels Möller | 299c839 | 2020-10-12 14:47:46 +0200 | [diff] [blame] | 57 | @CalledByNative("Buffer") |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 58 | Buffer cropAndScale( |
| 59 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Interface for I420 buffers. |
| 64 | */ |
| 65 | public interface I420Buffer extends Buffer { |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 66 | /** |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 67 | * Returns a direct ByteBuffer containing Y-plane data. The buffer capacity is at least |
| 68 | * getStrideY() * getHeight() bytes. The position of the returned buffer is ignored and must |
| 69 | * be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 70 | * implementations must return a new ByteBuffer or slice for each call. |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 71 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 72 | @CalledByNative("I420Buffer") ByteBuffer getDataY(); |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 73 | /** |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 74 | * Returns a direct ByteBuffer containing U-plane data. The buffer capacity is at least |
| 75 | * getStrideU() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored |
| 76 | * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 77 | * implementations must return a new ByteBuffer or slice for each call. |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 78 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 79 | @CalledByNative("I420Buffer") ByteBuffer getDataU(); |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 80 | /** |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 81 | * Returns a direct ByteBuffer containing V-plane data. The buffer capacity is at least |
| 82 | * getStrideV() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored |
| 83 | * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 84 | * implementations must return a new ByteBuffer or slice for each call. |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 85 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 86 | @CalledByNative("I420Buffer") ByteBuffer getDataV(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 87 | |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 88 | @CalledByNative("I420Buffer") int getStrideY(); |
| 89 | @CalledByNative("I420Buffer") int getStrideU(); |
| 90 | @CalledByNative("I420Buffer") int getStrideV(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Interface for buffers that are stored as a single texture, either in OES or RGB format. |
| 95 | */ |
| 96 | public interface TextureBuffer extends Buffer { |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 97 | enum Type { |
| 98 | OES(GLES11Ext.GL_TEXTURE_EXTERNAL_OES), |
| 99 | RGB(GLES20.GL_TEXTURE_2D); |
| 100 | |
| 101 | private final int glTarget; |
| 102 | |
| 103 | private Type(final int glTarget) { |
| 104 | this.glTarget = glTarget; |
| 105 | } |
| 106 | |
| 107 | public int getGlTarget() { |
| 108 | return glTarget; |
| 109 | } |
| 110 | } |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 111 | |
| 112 | Type getType(); |
| 113 | int getTextureId(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Retrieve the transform matrix associated with the frame. This transform matrix maps 2D |
| 117 | * homogeneous coordinates of the form (s, t, 1) with s and t in the inclusive range [0, 1] to |
| 118 | * the coordinate that should be used to sample that location from the buffer. |
| 119 | */ |
Magnus Jedvert | 783c6e3 | 2018-07-05 13:34:17 +0200 | [diff] [blame] | 120 | Matrix getTransformMatrix(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | private final Buffer buffer; |
| 124 | private final int rotation; |
| 125 | private final long timestampNs; |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 126 | |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 127 | /** |
| 128 | * Constructs a new VideoFrame backed by the given {@code buffer}. |
| 129 | * |
| 130 | * @note Ownership of the buffer object is tranferred to the new VideoFrame. |
| 131 | */ |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 132 | @CalledByNative |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 133 | public VideoFrame(Buffer buffer, int rotation, long timestampNs) { |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 134 | if (buffer == null) { |
| 135 | throw new IllegalArgumentException("buffer not allowed to be null"); |
| 136 | } |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 137 | if (rotation % 90 != 0) { |
| 138 | throw new IllegalArgumentException("rotation must be a multiple of 90"); |
| 139 | } |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 140 | this.buffer = buffer; |
| 141 | this.rotation = rotation; |
| 142 | this.timestampNs = timestampNs; |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 145 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 146 | public Buffer getBuffer() { |
| 147 | return buffer; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Rotation of the frame in degrees. |
| 152 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 153 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 154 | public int getRotation() { |
| 155 | return rotation; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Timestamp of the frame in nano seconds. |
| 160 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 161 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 162 | public long getTimestampNs() { |
| 163 | return timestampNs; |
| 164 | } |
| 165 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 166 | public int getRotatedWidth() { |
| 167 | if (rotation % 180 == 0) { |
| 168 | return buffer.getWidth(); |
| 169 | } |
| 170 | return buffer.getHeight(); |
| 171 | } |
| 172 | |
| 173 | public int getRotatedHeight() { |
| 174 | if (rotation % 180 == 0) { |
| 175 | return buffer.getHeight(); |
| 176 | } |
| 177 | return buffer.getWidth(); |
| 178 | } |
| 179 | |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 180 | @Override |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 181 | public void retain() { |
| 182 | buffer.retain(); |
| 183 | } |
| 184 | |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 185 | @Override |
Sami Kalliomäki | 2bde850 | 2018-02-15 13:58:15 +0100 | [diff] [blame] | 186 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 187 | public void release() { |
| 188 | buffer.release(); |
| 189 | } |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 190 | } |