blob: 49c75facfda9e382f1f3fd0d35e24aa5e9c06830 [file] [log] [blame]
magjed55220212017-06-02 02:45:56 -07001/*
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
11package org.webrtc;
12
13import android.graphics.Matrix;
Sami Kalliomäkicb98b112017-10-16 11:20:26 +020014import android.opengl.GLES11Ext;
15import android.opengl.GLES20;
magjed55220212017-06-02 02:45:56 -070016import 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äki61db3fd2018-04-09 17:51:19 +020028public 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 {
magjed55220212017-06-02 02:45:56 -070037 /**
38 * Resolution of the buffer in pixels.
39 */
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +010040 @CalledByNative("Buffer") int getWidth();
41 @CalledByNative("Buffer") int getHeight();
magjed55220212017-06-02 02:45:56 -070042
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 Jedvertc2ac3c62017-11-14 17:08:59 +010048 @CalledByNative("Buffer") I420Buffer toI420();
magjed55220212017-06-02 02:45:56 -070049
Sami Kalliomäki61db3fd2018-04-09 17:51:19 +020050 @Override @CalledByNative("Buffer") void retain();
51 @Override @CalledByNative("Buffer") void release();
sakal836f60c2017-07-28 07:12:23 -070052
53 /**
54 * Crops a region defined by |cropx|, |cropY|, |cropWidth| and |cropHeight|. Scales it to size
55 * |scaleWidth| x |scaleHeight|.
56 */
57 Buffer cropAndScale(
58 int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight);
magjed55220212017-06-02 02:45:56 -070059 }
60
61 /**
62 * Interface for I420 buffers.
63 */
64 public interface I420Buffer extends Buffer {
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020065 /**
Sami Kalliomäkie3044fe2017-10-02 09:41:55 +020066 * Returns a direct ByteBuffer containing Y-plane data. The buffer capacity is at least
67 * getStrideY() * getHeight() bytes. The position of the returned buffer is ignored and must
68 * be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so
69 * implementations must return a new ByteBuffer or slice for each call.
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020070 */
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +010071 @CalledByNative("I420Buffer") ByteBuffer getDataY();
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020072 /**
Sami Kalliomäkie3044fe2017-10-02 09:41:55 +020073 * Returns a direct ByteBuffer containing U-plane data. The buffer capacity is at least
74 * getStrideU() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored
75 * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so
76 * implementations must return a new ByteBuffer or slice for each call.
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020077 */
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +010078 @CalledByNative("I420Buffer") ByteBuffer getDataU();
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020079 /**
Sami Kalliomäkie3044fe2017-10-02 09:41:55 +020080 * Returns a direct ByteBuffer containing V-plane data. The buffer capacity is at least
81 * getStrideV() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored
82 * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so
83 * implementations must return a new ByteBuffer or slice for each call.
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020084 */
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +010085 @CalledByNative("I420Buffer") ByteBuffer getDataV();
magjed55220212017-06-02 02:45:56 -070086
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +010087 @CalledByNative("I420Buffer") int getStrideY();
88 @CalledByNative("I420Buffer") int getStrideU();
89 @CalledByNative("I420Buffer") int getStrideV();
magjed55220212017-06-02 02:45:56 -070090 }
91
92 /**
93 * Interface for buffers that are stored as a single texture, either in OES or RGB format.
94 */
95 public interface TextureBuffer extends Buffer {
Sami Kalliomäkicb98b112017-10-16 11:20:26 +020096 enum Type {
97 OES(GLES11Ext.GL_TEXTURE_EXTERNAL_OES),
98 RGB(GLES20.GL_TEXTURE_2D);
99
100 private final int glTarget;
101
102 private Type(final int glTarget) {
103 this.glTarget = glTarget;
104 }
105
106 public int getGlTarget() {
107 return glTarget;
108 }
109 }
magjed55220212017-06-02 02:45:56 -0700110
111 Type getType();
112 int getTextureId();
sakal836f60c2017-07-28 07:12:23 -0700113
114 /**
115 * Retrieve the transform matrix associated with the frame. This transform matrix maps 2D
116 * homogeneous coordinates of the form (s, t, 1) with s and t in the inclusive range [0, 1] to
117 * the coordinate that should be used to sample that location from the buffer.
118 */
Magnus Jedvert783c6e32018-07-05 13:34:17 +0200119 Matrix getTransformMatrix();
magjed55220212017-06-02 02:45:56 -0700120 }
121
122 private final Buffer buffer;
123 private final int rotation;
124 private final long timestampNs;
magjed55220212017-06-02 02:45:56 -0700125
Sami Kalliomäki61db3fd2018-04-09 17:51:19 +0200126 /**
127 * Constructs a new VideoFrame backed by the given {@code buffer}.
128 *
129 * @note Ownership of the buffer object is tranferred to the new VideoFrame.
130 */
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +0100131 @CalledByNative
sakal836f60c2017-07-28 07:12:23 -0700132 public VideoFrame(Buffer buffer, int rotation, long timestampNs) {
magjed55220212017-06-02 02:45:56 -0700133 if (buffer == null) {
134 throw new IllegalArgumentException("buffer not allowed to be null");
135 }
sakal6bdcefc2017-08-15 01:56:02 -0700136 if (rotation % 90 != 0) {
137 throw new IllegalArgumentException("rotation must be a multiple of 90");
138 }
magjed55220212017-06-02 02:45:56 -0700139 this.buffer = buffer;
140 this.rotation = rotation;
141 this.timestampNs = timestampNs;
magjed55220212017-06-02 02:45:56 -0700142 }
143
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +0100144 @CalledByNative
magjed55220212017-06-02 02:45:56 -0700145 public Buffer getBuffer() {
146 return buffer;
147 }
148
149 /**
150 * Rotation of the frame in degrees.
151 */
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +0100152 @CalledByNative
magjed55220212017-06-02 02:45:56 -0700153 public int getRotation() {
154 return rotation;
155 }
156
157 /**
158 * Timestamp of the frame in nano seconds.
159 */
Magnus Jedvertc2ac3c62017-11-14 17:08:59 +0100160 @CalledByNative
magjed55220212017-06-02 02:45:56 -0700161 public long getTimestampNs() {
162 return timestampNs;
163 }
164
sakal6bdcefc2017-08-15 01:56:02 -0700165 public int getRotatedWidth() {
166 if (rotation % 180 == 0) {
167 return buffer.getWidth();
168 }
169 return buffer.getHeight();
170 }
171
172 public int getRotatedHeight() {
173 if (rotation % 180 == 0) {
174 return buffer.getHeight();
175 }
176 return buffer.getWidth();
177 }
178
Sami Kalliomäki61db3fd2018-04-09 17:51:19 +0200179 @Override
magjed55220212017-06-02 02:45:56 -0700180 public void retain() {
181 buffer.retain();
182 }
183
Sami Kalliomäki61db3fd2018-04-09 17:51:19 +0200184 @Override
Sami Kalliomäki2bde8502018-02-15 13:58:15 +0100185 @CalledByNative
magjed55220212017-06-02 02:45:56 -0700186 public void release() {
187 buffer.release();
188 }
magjed55220212017-06-02 02:45:56 -0700189}