blob: 682d9c4fa02fc4a28197611cf76e722d70c26746 [file] [log] [blame]
Sami Kalliomäkie2410e92017-06-02 14:46:12 +02001/*
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
Niels Möller6fd67f02019-06-13 14:37:24 +020013import android.support.annotation.Nullable;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020014import java.nio.ByteBuffer;
sakale172d892017-08-31 02:37:28 -070015import java.util.concurrent.TimeUnit;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020016
17/**
18 * An encoded frame from a video stream. Used as an input for decoders and as an output for
19 * encoders.
20 */
Niels Möller67309ef2019-09-23 12:47:16 +020021public class EncodedImage implements RefCounted {
sakal07a3bd72017-09-04 03:57:21 -070022 // Must be kept in sync with common_types.h FrameType.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020023 public enum FrameType {
sakal07a3bd72017-09-04 03:57:21 -070024 EmptyFrame(0),
25 VideoFrameKey(3),
26 VideoFrameDelta(4);
27
28 private final int nativeIndex;
29
30 private FrameType(int nativeIndex) {
31 this.nativeIndex = nativeIndex;
32 }
33
34 public int getNative() {
35 return nativeIndex;
36 }
Magnus Jedvert4eb01882017-11-20 22:33:40 +010037
Magnus Jedvert4eb01882017-11-20 22:33:40 +010038 @CalledByNative("FrameType")
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010039 static FrameType fromNativeIndex(int nativeIndex) {
Magnus Jedvert4eb01882017-11-20 22:33:40 +010040 for (FrameType type : FrameType.values()) {
41 if (type.getNative() == nativeIndex) {
42 return type;
43 }
44 }
45 throw new IllegalArgumentException("Unknown native frame type: " + nativeIndex);
46 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020047 }
48
Niels Möller67309ef2019-09-23 12:47:16 +020049 private final RefCountDelegate refCountDelegate;
50 private final boolean supportsRetain;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020051 public final ByteBuffer buffer;
52 public final int encodedWidth;
53 public final int encodedHeight;
sakale172d892017-08-31 02:37:28 -070054 public final long captureTimeMs; // Deprecated
55 public final long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020056 public final FrameType frameType;
57 public final int rotation;
58 public final boolean completeFrame;
Niels Möller6fd67f02019-06-13 14:37:24 +020059 public final @Nullable Integer qp;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020060
Niels Möller67309ef2019-09-23 12:47:16 +020061 // TODO(bugs.webrtc.org/9378): Use retain and release from jni code.
62 @Override
63 public void retain() {
64 refCountDelegate.retain();
65 }
66
67 @Override
68 public void release() {
69 refCountDelegate.release();
70 }
71
72 // A false return value means that the encoder expects that the buffer is no longer used after
73 // VideoEncoder.Callback.onEncodedFrame returns.
74 boolean maybeRetain() {
75 if (supportsRetain) {
76 retain();
77 return true;
78 } else {
79 return false;
80 }
81 }
82
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010083 @CalledByNative
Niels Möller67309ef2019-09-23 12:47:16 +020084 private EncodedImage(ByteBuffer buffer, boolean supportsRetain,
85 @Nullable Runnable releaseCallback, int encodedWidth, int encodedHeight, long captureTimeNs,
Niels Möller6fd67f02019-06-13 14:37:24 +020086 FrameType frameType, int rotation, boolean completeFrame, @Nullable Integer qp) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020087 this.buffer = buffer;
88 this.encodedWidth = encodedWidth;
89 this.encodedHeight = encodedHeight;
sakale172d892017-08-31 02:37:28 -070090 this.captureTimeMs = TimeUnit.NANOSECONDS.toMillis(captureTimeNs);
91 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020092 this.frameType = frameType;
93 this.rotation = rotation;
94 this.completeFrame = completeFrame;
95 this.qp = qp;
Niels Möller67309ef2019-09-23 12:47:16 +020096 this.supportsRetain = supportsRetain;
97 this.refCountDelegate = new RefCountDelegate(releaseCallback);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020098 }
99
Niels Möller6fd67f02019-06-13 14:37:24 +0200100 @CalledByNative
101 private ByteBuffer getBuffer() {
102 return buffer;
103 }
104
105 @CalledByNative
106 private int getEncodedWidth() {
107 return encodedWidth;
108 }
109
110 @CalledByNative
111 private int getEncodedHeight() {
112 return encodedHeight;
113 }
114
115 @CalledByNative
116 private long getCaptureTimeNs() {
117 return captureTimeNs;
118 }
119
120 @CalledByNative
121 private int getFrameType() {
122 return frameType.getNative();
123 }
124
125 @CalledByNative
126 private int getRotation() {
127 return rotation;
128 }
129
130 @CalledByNative
131 private boolean getCompleteFrame() {
132 return completeFrame;
133 }
134
135 @CalledByNative
136 private @Nullable Integer getQp() {
137 return qp;
138 }
139
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -0700140 public static Builder builder() {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200141 return new Builder();
142 }
143
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -0700144 public static class Builder {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200145 private ByteBuffer buffer;
Niels Möller67309ef2019-09-23 12:47:16 +0200146 private boolean supportsRetain;
147 private @Nullable Runnable releaseCallback;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200148 private int encodedWidth;
149 private int encodedHeight;
sakale172d892017-08-31 02:37:28 -0700150 private long captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200151 private EncodedImage.FrameType frameType;
152 private int rotation;
153 private boolean completeFrame;
Niels Möller6fd67f02019-06-13 14:37:24 +0200154 private @Nullable Integer qp;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200155
156 private Builder() {}
157
Niels Möller67309ef2019-09-23 12:47:16 +0200158 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200159 public Builder setBuffer(ByteBuffer buffer) {
160 this.buffer = buffer;
Niels Möller67309ef2019-09-23 12:47:16 +0200161 this.releaseCallback = null;
162 this.supportsRetain = false;
163 return this;
164 }
165
166 public Builder setBuffer(ByteBuffer buffer, @Nullable Runnable releaseCallback) {
167 this.buffer = buffer;
168 this.releaseCallback = releaseCallback;
169 this.supportsRetain = true;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200170 return this;
171 }
172
173 public Builder setEncodedWidth(int encodedWidth) {
174 this.encodedWidth = encodedWidth;
175 return this;
176 }
177
178 public Builder setEncodedHeight(int encodedHeight) {
179 this.encodedHeight = encodedHeight;
180 return this;
181 }
182
sakale172d892017-08-31 02:37:28 -0700183 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200184 public Builder setCaptureTimeMs(long captureTimeMs) {
sakale172d892017-08-31 02:37:28 -0700185 this.captureTimeNs = TimeUnit.MILLISECONDS.toNanos(captureTimeMs);
186 return this;
187 }
188
189 public Builder setCaptureTimeNs(long captureTimeNs) {
190 this.captureTimeNs = captureTimeNs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200191 return this;
192 }
193
194 public Builder setFrameType(EncodedImage.FrameType frameType) {
195 this.frameType = frameType;
196 return this;
197 }
198
199 public Builder setRotation(int rotation) {
200 this.rotation = rotation;
201 return this;
202 }
203
204 public Builder setCompleteFrame(boolean completeFrame) {
205 this.completeFrame = completeFrame;
206 return this;
207 }
208
Niels Möller6fd67f02019-06-13 14:37:24 +0200209 public Builder setQp(@Nullable Integer qp) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200210 this.qp = qp;
211 return this;
212 }
213
214 public EncodedImage createEncodedImage() {
Niels Möller67309ef2019-09-23 12:47:16 +0200215 return new EncodedImage(buffer, supportsRetain, releaseCallback, encodedWidth, encodedHeight,
216 captureTimeNs, frameType, rotation, completeFrame, qp);
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200217 }
218 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200219}