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