blob: 311a3ecaf5bf5da6b200d47755cab659a591a1b3 [file] [log] [blame]
fischman@webrtc.org540acde2014-02-13 03:56:14 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
fischman@webrtc.org540acde2014-02-13 03:56:14 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
fischman@webrtc.org540acde2014-02-13 03:56:14 +00009 */
10
fischman@webrtc.org540acde2014-02-13 03:56:14 +000011package org.webrtc;
12
Patrik Höglund68876f92015-11-12 17:36:48 +010013import android.annotation.TargetApi;
sakalb5f5bdc2017-08-10 04:15:42 -070014import android.graphics.Matrix;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000015import android.media.MediaCodec;
16import android.media.MediaCodecInfo;
Sami Kalliomakid3235f02016-08-02 15:44:04 +020017import android.media.MediaCodecInfo.CodecCapabilities;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000018import android.media.MediaCodecList;
19import android.media.MediaFormat;
perkj30e91822015-11-20 01:31:25 -080020import android.opengl.GLES20;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000021import android.os.Build;
22import android.os.Bundle;
perkj30e91822015-11-20 01:31:25 -080023import android.view.Surface;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000024import java.nio.ByteBuffer;
magjed295760d2017-01-12 01:11:57 -080025import java.util.ArrayList;
Alex Glaznev0c850202015-08-04 10:26:59 -070026import java.util.Arrays;
Alex Glazneveee86a62016-01-29 14:17:07 -080027import java.util.HashSet;
Alex Glaznev0c850202015-08-04 10:26:59 -070028import java.util.List;
Alex Glazneveee86a62016-01-29 14:17:07 -080029import java.util.Set;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070030import java.util.concurrent.CountDownLatch;
perkj48477c12015-12-18 00:34:37 -080031import java.util.concurrent.TimeUnit;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010032import javax.annotation.Nullable;
Magnus Jedvert0f0e7a62018-07-11 14:53:21 +020033import org.webrtc.EglBase;
Magnus Jedvert655e1962017-12-08 11:05:22 +010034import org.webrtc.EglBase14;
35import org.webrtc.VideoFrame;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000036
Magnus Jedvert9060eb12017-12-12 12:52:54 +010037// Java-side of peerconnection.cc:MediaCodecVideoEncoder.
fischman@webrtc.org540acde2014-02-13 03:56:14 +000038// This class is an implementation detail of the Java PeerConnection API.
Patrik Höglund68876f92015-11-12 17:36:48 +010039@TargetApi(19)
40@SuppressWarnings("deprecation")
Magnus Jedvert0f0e7a62018-07-11 14:53:21 +020041@Deprecated
perkj@webrtc.org47098872014-10-24 11:38:19 +000042public class MediaCodecVideoEncoder {
fischman@webrtc.org540acde2014-02-13 03:56:14 +000043 // This class is constructed, operated, and destroyed by its C++ incarnation,
44 // so the class and its methods have non-public visibility. The API this
45 // class exposes aims to mimic the webrtc::VideoEncoder API as closely as
46 // possibly to minimize the amount of translation work necessary.
47
48 private static final String TAG = "MediaCodecVideoEncoder";
49
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000050 // Tracks webrtc::VideoCodecType.
Magnus Jedvert655e1962017-12-08 11:05:22 +010051 public enum VideoCodecType {
Niels Möller520ca4e2018-06-04 11:14:38 +020052 VIDEO_CODEC_UNKNOWN,
Magnus Jedvert655e1962017-12-08 11:05:22 +010053 VIDEO_CODEC_VP8,
54 VIDEO_CODEC_VP9,
55 VIDEO_CODEC_H264;
56
57 @CalledByNative("VideoCodecType")
58 static VideoCodecType fromNativeIndex(int nativeIndex) {
59 return values()[nativeIndex];
60 }
61 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000062
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070063 private static final int MEDIA_CODEC_RELEASE_TIMEOUT_MS = 5000; // Timeout for codec releasing.
sakalb6760f92016-09-29 04:12:44 -070064 private static final int DEQUEUE_TIMEOUT = 0; // Non-blocking, no wait.
Alex Glaznev269fe752016-05-25 16:17:33 -070065 private static final int BITRATE_ADJUSTMENT_FPS = 30;
Alex Glaznevc55c39d2016-09-02 12:16:27 -070066 private static final int MAXIMUM_INITIAL_FPS = 30;
Alex Glaznevcfaca032016-09-06 14:08:19 -070067 private static final double BITRATE_CORRECTION_SEC = 3.0;
Alex Glaznev7fa4a722017-01-17 15:32:02 -080068 // Maximum bitrate correction scale - no more than 4 times.
69 private static final double BITRATE_CORRECTION_MAX_SCALE = 4;
Alex Glaznevcfaca032016-09-06 14:08:19 -070070 // Amount of correction steps to reach correction maximum scale.
Alex Glaznev7fa4a722017-01-17 15:32:02 -080071 private static final int BITRATE_CORRECTION_STEPS = 20;
Alex Glaznevc7483a72017-01-05 15:22:24 -080072 // Forced key frame interval - used to reduce color distortions on Qualcomm platform.
alexlau84ee5c62017-06-02 17:36:32 -070073 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_L_MS = 15000;
74 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS = 20000;
Alex Glaznevc7483a72017-01-05 15:22:24 -080075 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS = 15000;
Alex Glaznevcfaca032016-09-06 14:08:19 -070076
perkj9576e542015-11-12 06:43:16 -080077 // Active running encoder instance. Set in initEncode() (called from native code)
Alex Glaznevc6aec4b2015-10-19 16:39:19 -070078 // and reset to null in release() call.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010079 @Nullable private static MediaCodecVideoEncoder runningInstance = null;
80 @Nullable private static MediaCodecVideoEncoderErrorCallback errorCallback = null;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070081 private static int codecErrors = 0;
Alex Glazneveee86a62016-01-29 14:17:07 -080082 // List of disabled codec types - can be set from application.
83 private static Set<String> hwEncoderDisabledTypes = new HashSet<String>();
Magnus Jedvert0f0e7a62018-07-11 14:53:21 +020084 @Nullable private static EglBase staticEglBase;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070085
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010086 @Nullable private Thread mediaCodecThread;
87 @Nullable private MediaCodec mediaCodec;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000088 private ByteBuffer[] outputBuffers;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010089 @Nullable private EglBase14 eglBase;
glaznev3fc23502017-06-15 16:24:37 -070090 private int profile;
Magnus Jedvert51254332015-12-15 16:22:29 +010091 private int width;
92 private int height;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010093 @Nullable private Surface inputSurface;
94 @Nullable private GlRectDrawer drawer;
Alex Glaznev269fe752016-05-25 16:17:33 -070095
fischman@webrtc.org540acde2014-02-13 03:56:14 +000096 private static final String VP8_MIME_TYPE = "video/x-vnd.on2.vp8";
Alex Glaznevad948c42015-11-18 13:06:42 -080097 private static final String VP9_MIME_TYPE = "video/x-vnd.on2.vp9";
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000098 private static final String H264_MIME_TYPE = "video/avc";
Alex Glaznev269fe752016-05-25 16:17:33 -070099
glaznev3fc23502017-06-15 16:24:37 -0700100 private static final int VIDEO_AVCProfileHigh = 8;
101 private static final int VIDEO_AVCLevel3 = 0x100;
102
Alex Glaznevcfaca032016-09-06 14:08:19 -0700103 // Type of bitrate adjustment for video encoder.
104 public enum BitrateAdjustmentType {
105 // No adjustment - video encoder has no known bitrate problem.
106 NO_ADJUSTMENT,
107 // Framerate based bitrate adjustment is required - HW encoder does not use frame
108 // timestamps to calculate frame bitrate budget and instead is relying on initial
109 // fps configuration assuming that all frames are coming at fixed initial frame rate.
110 FRAMERATE_ADJUSTMENT,
111 // Dynamic bitrate adjustment is required - HW encoder used frame timestamps, but actual
112 // bitrate deviates too much from the target value.
113 DYNAMIC_ADJUSTMENT
114 }
115
glaznev3fc23502017-06-15 16:24:37 -0700116 // Should be in sync with webrtc::H264::Profile.
117 public static enum H264Profile {
118 CONSTRAINED_BASELINE(0),
119 BASELINE(1),
120 MAIN(2),
121 CONSTRAINED_HIGH(3),
122 HIGH(4);
123
124 private final int value;
125
126 H264Profile(int value) {
127 this.value = value;
128 }
129
130 public int getValue() {
131 return value;
132 }
133 }
134
Alex Glaznev269fe752016-05-25 16:17:33 -0700135 // Class describing supported media codec properties.
136 private static class MediaCodecProperties {
137 public final String codecPrefix;
138 // Minimum Android SDK required for this codec to be used.
139 public final int minSdk;
140 // Flag if encoder implementation does not use frame timestamps to calculate frame bitrate
141 // budget and instead is relying on initial fps configuration assuming that all frames are
142 // coming at fixed initial frame rate. Bitrate adjustment is required for this case.
Alex Glaznevcfaca032016-09-06 14:08:19 -0700143 public final BitrateAdjustmentType bitrateAdjustmentType;
Alex Glaznev269fe752016-05-25 16:17:33 -0700144
145 MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700146 String codecPrefix, int minSdk, BitrateAdjustmentType bitrateAdjustmentType) {
Alex Glaznev269fe752016-05-25 16:17:33 -0700147 this.codecPrefix = codecPrefix;
148 this.minSdk = minSdk;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700149 this.bitrateAdjustmentType = bitrateAdjustmentType;
Alex Glaznev269fe752016-05-25 16:17:33 -0700150 }
151 }
152
Magnus Jedvert0f0e7a62018-07-11 14:53:21 +0200153 /**
154 * Set EGL context used by HW encoding. The EGL context must be shared with the video capturer
155 * and any local render.
156 */
157 public static void setEglContext(EglBase.Context eglContext) {
158 if (staticEglBase != null) {
159 Logging.w(TAG, "Egl context already set.");
160 staticEglBase.release();
161 }
162 staticEglBase = EglBase.create(eglContext);
163 }
164
165 /** Dispose the EGL context used by HW encoding. */
166 public static void disposeEglContext() {
167 if (staticEglBase != null) {
168 staticEglBase.release();
169 staticEglBase = null;
170 }
171 }
172
173 @Nullable
174 static EglBase.Context getEglContext() {
175 return staticEglBase == null ? null : staticEglBase.getEglBaseContext();
176 }
177
178 @CalledByNative
179 static boolean hasEgl14Context() {
180 return staticEglBase instanceof EglBase14;
181 }
182
Alex Glaznevdcd730f2016-04-21 17:01:46 -0700183 // List of supported HW VP8 encoders.
Alex Glaznev269fe752016-05-25 16:17:33 -0700184 private static final MediaCodecProperties qcomVp8HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700185 "OMX.qcom.", Build.VERSION_CODES.KITKAT, BitrateAdjustmentType.NO_ADJUSTMENT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700186 private static final MediaCodecProperties exynosVp8HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700187 "OMX.Exynos.", Build.VERSION_CODES.M, BitrateAdjustmentType.DYNAMIC_ADJUSTMENT);
magjed295760d2017-01-12 01:11:57 -0800188 private static final MediaCodecProperties intelVp8HwProperties = new MediaCodecProperties(
189 "OMX.Intel.", Build.VERSION_CODES.LOLLIPOP, BitrateAdjustmentType.NO_ADJUSTMENT);
190 private static MediaCodecProperties[] vp8HwList() {
191 final ArrayList<MediaCodecProperties> supported_codecs = new ArrayList<MediaCodecProperties>();
192 supported_codecs.add(qcomVp8HwProperties);
193 supported_codecs.add(exynosVp8HwProperties);
194 if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-IntelVP8").equals("Enabled")) {
195 supported_codecs.add(intelVp8HwProperties);
196 }
197 return supported_codecs.toArray(new MediaCodecProperties[supported_codecs.size()]);
198 }
Alex Glaznev269fe752016-05-25 16:17:33 -0700199
Alex Glaznevdcd730f2016-04-21 17:01:46 -0700200 // List of supported HW VP9 encoders.
Alex Glaznev269fe752016-05-25 16:17:33 -0700201 private static final MediaCodecProperties qcomVp9HwProperties = new MediaCodecProperties(
glaznev6fac4292017-06-02 20:18:54 -0700202 "OMX.qcom.", Build.VERSION_CODES.N, BitrateAdjustmentType.NO_ADJUSTMENT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700203 private static final MediaCodecProperties exynosVp9HwProperties = new MediaCodecProperties(
glaznev6fac4292017-06-02 20:18:54 -0700204 "OMX.Exynos.", Build.VERSION_CODES.N, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
sakalb6760f92016-09-29 04:12:44 -0700205 private static final MediaCodecProperties[] vp9HwList =
206 new MediaCodecProperties[] {qcomVp9HwProperties, exynosVp9HwProperties};
Alex Glaznev269fe752016-05-25 16:17:33 -0700207
Alex Glaznevdcd730f2016-04-21 17:01:46 -0700208 // List of supported HW H.264 encoders.
Alex Glaznev269fe752016-05-25 16:17:33 -0700209 private static final MediaCodecProperties qcomH264HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700210 "OMX.qcom.", Build.VERSION_CODES.KITKAT, BitrateAdjustmentType.NO_ADJUSTMENT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700211 private static final MediaCodecProperties exynosH264HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700212 "OMX.Exynos.", Build.VERSION_CODES.LOLLIPOP, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
Alex Leung5b6891a2018-01-18 10:01:14 -0800213 private static final MediaCodecProperties mediatekH264HwProperties = new MediaCodecProperties(
Alex Leung28e71072018-02-05 13:42:48 -0800214 "OMX.MTK.", Build.VERSION_CODES.O_MR1, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
Alex Leung5b6891a2018-01-18 10:01:14 -0800215 private static final MediaCodecProperties[] h264HwList() {
216 final ArrayList<MediaCodecProperties> supported_codecs = new ArrayList<MediaCodecProperties>();
217 supported_codecs.add(qcomH264HwProperties);
218 supported_codecs.add(exynosH264HwProperties);
219 if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-MediaTekH264").equals("Enabled")) {
220 supported_codecs.add(mediatekH264HwProperties);
221 }
222 return supported_codecs.toArray(new MediaCodecProperties[supported_codecs.size()]);
223 }
Alex Glaznev269fe752016-05-25 16:17:33 -0700224
glaznev3fc23502017-06-15 16:24:37 -0700225 // List of supported HW H.264 high profile encoders.
226 private static final MediaCodecProperties exynosH264HighProfileHwProperties =
227 new MediaCodecProperties(
228 "OMX.Exynos.", Build.VERSION_CODES.M, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
229 private static final MediaCodecProperties[] h264HighProfileHwList =
230 new MediaCodecProperties[] {exynosH264HighProfileHwProperties};
231
Alex Glaznev0c850202015-08-04 10:26:59 -0700232 // List of devices with poor H.264 encoder quality.
sakalb6760f92016-09-29 04:12:44 -0700233 // HW H.264 encoder on below devices has poor bitrate control - actual
234 // bitrates deviates a lot from the target value.
235 private static final String[] H264_HW_EXCEPTION_MODELS =
236 new String[] {"SAMSUNG-SGH-I337", "Nexus 7", "Nexus 4"};
Alex Glaznev0c850202015-08-04 10:26:59 -0700237
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000238 // Bitrate modes - should be in sync with OMX_VIDEO_CONTROLRATETYPE defined
239 // in OMX_Video.h
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000240 private static final int VIDEO_ControlRateConstant = 2;
241 // NV12 color format supported by QCOM codec, but not declared in MediaCodec -
242 // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h
sakalb6760f92016-09-29 04:12:44 -0700243 private static final int COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04;
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000244 // Allowable color formats supported by codec - in order of preference.
sakalb6760f92016-09-29 04:12:44 -0700245 private static final int[] supportedColorList = {CodecCapabilities.COLOR_FormatYUV420Planar,
246 CodecCapabilities.COLOR_FormatYUV420SemiPlanar,
247 CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar,
248 COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m};
249 private static final int[] supportedSurfaceColorList = {CodecCapabilities.COLOR_FormatSurface};
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000250 private VideoCodecType type;
Magnus Jedvert655e1962017-12-08 11:05:22 +0100251 private int colorFormat;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700252
253 // Variables used for dynamic bitrate adjustment.
254 private BitrateAdjustmentType bitrateAdjustmentType = BitrateAdjustmentType.NO_ADJUSTMENT;
255 private double bitrateAccumulator;
256 private double bitrateAccumulatorMax;
257 private double bitrateObservationTimeMs;
258 private int bitrateAdjustmentScaleExp;
259 private int targetBitrateBps;
260 private int targetFps;
perkj9576e542015-11-12 06:43:16 -0800261
Alex Glaznevc7483a72017-01-05 15:22:24 -0800262 // Interval in ms to force key frame generation. Used to reduce the time of color distortions
263 // happened sometime when using Qualcomm video encoder.
264 private long forcedKeyFrameMs;
265 private long lastKeyFrameMs;
266
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000267 // SPS and PPS NALs (Config frame) for H.264.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100268 @Nullable private ByteBuffer configData = null;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000269
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700270 // MediaCodec error handler - invoked when critical error happens which may prevent
271 // further use of media codec API. Now it means that one of media codec instances
272 // is hanging and can no longer be used in the next call.
273 public static interface MediaCodecVideoEncoderErrorCallback {
274 void onMediaCodecVideoEncoderCriticalError(int codecErrors);
275 }
276
277 public static void setErrorCallback(MediaCodecVideoEncoderErrorCallback errorCallback) {
278 Logging.d(TAG, "Set error callback");
279 MediaCodecVideoEncoder.errorCallback = errorCallback;
280 }
281
Alex Glazneveee86a62016-01-29 14:17:07 -0800282 // Functions to disable HW encoding - can be called from applications for platforms
283 // which have known HW decoding problems.
284 public static void disableVp8HwCodec() {
285 Logging.w(TAG, "VP8 encoding is disabled by application.");
286 hwEncoderDisabledTypes.add(VP8_MIME_TYPE);
287 }
288
289 public static void disableVp9HwCodec() {
290 Logging.w(TAG, "VP9 encoding is disabled by application.");
291 hwEncoderDisabledTypes.add(VP9_MIME_TYPE);
292 }
293
294 public static void disableH264HwCodec() {
295 Logging.w(TAG, "H.264 encoding is disabled by application.");
296 hwEncoderDisabledTypes.add(H264_MIME_TYPE);
297 }
298
299 // Functions to query if HW encoding is supported.
Magnus Jedvert655e1962017-12-08 11:05:22 +0100300 @CalledByNative
Alex Glazneveee86a62016-01-29 14:17:07 -0800301 public static boolean isVp8HwSupported() {
sakalb6760f92016-09-29 04:12:44 -0700302 return !hwEncoderDisabledTypes.contains(VP8_MIME_TYPE)
magjed295760d2017-01-12 01:11:57 -0800303 && (findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800304 }
305
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100306 public static @Nullable EncoderProperties vp8HwEncoderProperties() {
Alex Glaznevc7483a72017-01-05 15:22:24 -0800307 if (hwEncoderDisabledTypes.contains(VP8_MIME_TYPE)) {
308 return null;
309 } else {
magjed295760d2017-01-12 01:11:57 -0800310 return findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedColorList);
Alex Glaznevc7483a72017-01-05 15:22:24 -0800311 }
312 }
313
Magnus Jedvert655e1962017-12-08 11:05:22 +0100314 @CalledByNative
Alex Glazneveee86a62016-01-29 14:17:07 -0800315 public static boolean isVp9HwSupported() {
sakalb6760f92016-09-29 04:12:44 -0700316 return !hwEncoderDisabledTypes.contains(VP9_MIME_TYPE)
317 && (findHwEncoder(VP9_MIME_TYPE, vp9HwList, supportedColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800318 }
319
Magnus Jedvert655e1962017-12-08 11:05:22 +0100320 @CalledByNative
Alex Glazneveee86a62016-01-29 14:17:07 -0800321 public static boolean isH264HwSupported() {
sakalb6760f92016-09-29 04:12:44 -0700322 return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE)
Alex Leung5b6891a2018-01-18 10:01:14 -0800323 && (findHwEncoder(H264_MIME_TYPE, h264HwList(), supportedColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800324 }
325
glaznev3fc23502017-06-15 16:24:37 -0700326 public static boolean isH264HighProfileHwSupported() {
327 return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE)
328 && (findHwEncoder(H264_MIME_TYPE, h264HighProfileHwList, supportedColorList) != null);
329 }
330
Alex Glazneveee86a62016-01-29 14:17:07 -0800331 public static boolean isVp8HwSupportedUsingTextures() {
sakalb6760f92016-09-29 04:12:44 -0700332 return !hwEncoderDisabledTypes.contains(VP8_MIME_TYPE)
magjed295760d2017-01-12 01:11:57 -0800333 && (findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedSurfaceColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800334 }
335
336 public static boolean isVp9HwSupportedUsingTextures() {
sakalb6760f92016-09-29 04:12:44 -0700337 return !hwEncoderDisabledTypes.contains(VP9_MIME_TYPE)
338 && (findHwEncoder(VP9_MIME_TYPE, vp9HwList, supportedSurfaceColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800339 }
340
341 public static boolean isH264HwSupportedUsingTextures() {
sakalb6760f92016-09-29 04:12:44 -0700342 return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE)
Alex Leung5b6891a2018-01-18 10:01:14 -0800343 && (findHwEncoder(H264_MIME_TYPE, h264HwList(), supportedSurfaceColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800344 }
345
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000346 // Helper struct for findHwEncoder() below.
Alex Glaznevc7483a72017-01-05 15:22:24 -0800347 public static class EncoderProperties {
Alex Glaznevcfaca032016-09-06 14:08:19 -0700348 public EncoderProperties(
349 String codecName, int colorFormat, BitrateAdjustmentType bitrateAdjustmentType) {
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000350 this.codecName = codecName;
351 this.colorFormat = colorFormat;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700352 this.bitrateAdjustmentType = bitrateAdjustmentType;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000353 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000354 public final String codecName; // OpenMax component name for HW codec.
sakalb6760f92016-09-29 04:12:44 -0700355 public final int colorFormat; // Color format supported by codec.
Alex Glaznevcfaca032016-09-06 14:08:19 -0700356 public final BitrateAdjustmentType bitrateAdjustmentType; // Bitrate adjustment type
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000357 }
358
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100359 private static @Nullable EncoderProperties findHwEncoder(
Alex Glaznev269fe752016-05-25 16:17:33 -0700360 String mime, MediaCodecProperties[] supportedHwCodecProperties, int[] colorList) {
Alex Glaznev0c850202015-08-04 10:26:59 -0700361 // MediaCodec.setParameters is missing for JB and below, so bitrate
362 // can not be adjusted dynamically.
363 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
364 return null;
365 }
366
367 // Check if device is in H.264 exception list.
368 if (mime.equals(H264_MIME_TYPE)) {
369 List<String> exceptionModels = Arrays.asList(H264_HW_EXCEPTION_MODELS);
370 if (exceptionModels.contains(Build.MODEL)) {
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700371 Logging.w(TAG, "Model: " + Build.MODEL + " has black listed H.264 encoder.");
Alex Glaznev0c850202015-08-04 10:26:59 -0700372 return null;
373 }
374 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000375
376 for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) {
Alex Glaznev0060c562016-08-08 12:27:24 -0700377 MediaCodecInfo info = null;
378 try {
379 info = MediaCodecList.getCodecInfoAt(i);
380 } catch (IllegalArgumentException e) {
sakalb6760f92016-09-29 04:12:44 -0700381 Logging.e(TAG, "Cannot retrieve encoder codec info", e);
Alex Glaznev0060c562016-08-08 12:27:24 -0700382 }
383 if (info == null || !info.isEncoder()) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000384 continue;
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000385 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000386 String name = null;
387 for (String mimeType : info.getSupportedTypes()) {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000388 if (mimeType.equals(mime)) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000389 name = info.getName();
390 break;
391 }
392 }
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000393 if (name == null) {
sakalb6760f92016-09-29 04:12:44 -0700394 continue; // No HW support in this codec; try the next one.
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000395 }
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700396 Logging.v(TAG, "Found candidate encoder " + name);
glaznev@webrtc.org99678452014-09-15 17:52:42 +0000397
398 // Check if this is supported HW encoder.
399 boolean supportedCodec = false;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700400 BitrateAdjustmentType bitrateAdjustmentType = BitrateAdjustmentType.NO_ADJUSTMENT;
Alex Glaznev269fe752016-05-25 16:17:33 -0700401 for (MediaCodecProperties codecProperties : supportedHwCodecProperties) {
402 if (name.startsWith(codecProperties.codecPrefix)) {
403 if (Build.VERSION.SDK_INT < codecProperties.minSdk) {
sakalb6760f92016-09-29 04:12:44 -0700404 Logging.w(
405 TAG, "Codec " + name + " is disabled due to SDK version " + Build.VERSION.SDK_INT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700406 continue;
407 }
Alex Glaznevcfaca032016-09-06 14:08:19 -0700408 if (codecProperties.bitrateAdjustmentType != BitrateAdjustmentType.NO_ADJUSTMENT) {
409 bitrateAdjustmentType = codecProperties.bitrateAdjustmentType;
sakalb6760f92016-09-29 04:12:44 -0700410 Logging.w(
411 TAG, "Codec " + name + " requires bitrate adjustment: " + bitrateAdjustmentType);
Alex Glaznev269fe752016-05-25 16:17:33 -0700412 }
glaznev@webrtc.org99678452014-09-15 17:52:42 +0000413 supportedCodec = true;
414 break;
415 }
416 }
417 if (!supportedCodec) {
418 continue;
419 }
420
Alex Glaznev269fe752016-05-25 16:17:33 -0700421 // Check if HW codec supports known color format.
Alex Glaznev0060c562016-08-08 12:27:24 -0700422 CodecCapabilities capabilities;
423 try {
424 capabilities = info.getCapabilitiesForType(mime);
425 } catch (IllegalArgumentException e) {
sakalb6760f92016-09-29 04:12:44 -0700426 Logging.e(TAG, "Cannot retrieve encoder capabilities", e);
Alex Glaznev0060c562016-08-08 12:27:24 -0700427 continue;
428 }
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000429 for (int colorFormat : capabilities.colorFormats) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700430 Logging.v(TAG, " Color: 0x" + Integer.toHexString(colorFormat));
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000431 }
432
perkj30e91822015-11-20 01:31:25 -0800433 for (int supportedColorFormat : colorList) {
glaznev@webrtc.org99678452014-09-15 17:52:42 +0000434 for (int codecColorFormat : capabilities.colorFormats) {
435 if (codecColorFormat == supportedColorFormat) {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000436 // Found supported HW encoder.
sakalb6760f92016-09-29 04:12:44 -0700437 Logging.d(TAG, "Found target encoder for mime " + mime + " : " + name + ". Color: 0x"
438 + Integer.toHexString(codecColorFormat) + ". Bitrate adjustment: "
439 + bitrateAdjustmentType);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700440 return new EncoderProperties(name, codecColorFormat, bitrateAdjustmentType);
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000441 }
442 }
443 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000444 }
sakalb6760f92016-09-29 04:12:44 -0700445 return null; // No HW encoder.
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000446 }
447
Magnus Jedvert655e1962017-12-08 11:05:22 +0100448 @CalledByNative
449 MediaCodecVideoEncoder() {}
450
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000451 private void checkOnMediaCodecThread() {
452 if (mediaCodecThread.getId() != Thread.currentThread().getId()) {
sakalb6760f92016-09-29 04:12:44 -0700453 throw new RuntimeException("MediaCodecVideoEncoder previously operated on " + mediaCodecThread
454 + " but is now called on " + Thread.currentThread());
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000455 }
456 }
457
Alex Glaznev325d4142015-10-12 14:56:02 -0700458 public static void printStackTrace() {
Alex Glaznevc6aec4b2015-10-19 16:39:19 -0700459 if (runningInstance != null && runningInstance.mediaCodecThread != null) {
460 StackTraceElement[] mediaCodecStackTraces = runningInstance.mediaCodecThread.getStackTrace();
Alex Glaznev325d4142015-10-12 14:56:02 -0700461 if (mediaCodecStackTraces.length > 0) {
462 Logging.d(TAG, "MediaCodecVideoEncoder stacks trace:");
463 for (StackTraceElement stackTrace : mediaCodecStackTraces) {
464 Logging.d(TAG, stackTrace.toString());
465 }
466 }
467 }
468 }
469
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100470 static @Nullable MediaCodec createByCodecName(String codecName) {
henrike@webrtc.org528fc652014-10-06 17:56:43 +0000471 try {
472 // In the L-SDK this call can throw IOException so in order to work in
473 // both cases catch an exception.
474 return MediaCodec.createByCodecName(codecName);
475 } catch (Exception e) {
476 return null;
477 }
478 }
479
Magnus Jedvert655e1962017-12-08 11:05:22 +0100480 @CalledByNativeUnchecked
glaznev3fc23502017-06-15 16:24:37 -0700481 boolean initEncode(VideoCodecType type, int profile, int width, int height, int kbps, int fps,
Magnus Jedvert0f0e7a62018-07-11 14:53:21 +0200482 boolean useSurface) {
glaznev3fc23502017-06-15 16:24:37 -0700483 Logging.d(TAG,
484 "Java initEncode: " + type + ". Profile: " + profile + " : " + width + " x " + height
485 + ". @ " + kbps + " kbps. Fps: " + fps + ". Encode from texture : " + useSurface);
perkj9576e542015-11-12 06:43:16 -0800486
glaznev3fc23502017-06-15 16:24:37 -0700487 this.profile = profile;
Magnus Jedvert51254332015-12-15 16:22:29 +0100488 this.width = width;
489 this.height = height;
Alejandro Luebs69ddaef2015-10-09 15:46:09 -0700490 if (mediaCodecThread != null) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000491 throw new RuntimeException("Forgot to release()?");
492 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000493 EncoderProperties properties = null;
494 String mime = null;
495 int keyFrameIntervalSec = 0;
glaznev3fc23502017-06-15 16:24:37 -0700496 boolean configureH264HighProfile = false;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000497 if (type == VideoCodecType.VIDEO_CODEC_VP8) {
498 mime = VP8_MIME_TYPE;
Alex Glaznev269fe752016-05-25 16:17:33 -0700499 properties = findHwEncoder(
magjed295760d2017-01-12 01:11:57 -0800500 VP8_MIME_TYPE, vp8HwList(), useSurface ? supportedSurfaceColorList : supportedColorList);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000501 keyFrameIntervalSec = 100;
Alex Glaznevad948c42015-11-18 13:06:42 -0800502 } else if (type == VideoCodecType.VIDEO_CODEC_VP9) {
503 mime = VP9_MIME_TYPE;
Alex Glaznev269fe752016-05-25 16:17:33 -0700504 properties = findHwEncoder(
505 VP9_MIME_TYPE, vp9HwList, useSurface ? supportedSurfaceColorList : supportedColorList);
Alex Glaznevad948c42015-11-18 13:06:42 -0800506 keyFrameIntervalSec = 100;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000507 } else if (type == VideoCodecType.VIDEO_CODEC_H264) {
508 mime = H264_MIME_TYPE;
Alex Leung5b6891a2018-01-18 10:01:14 -0800509 properties = findHwEncoder(H264_MIME_TYPE, h264HwList(),
510 useSurface ? supportedSurfaceColorList : supportedColorList);
glaznev3fc23502017-06-15 16:24:37 -0700511 if (profile == H264Profile.CONSTRAINED_HIGH.getValue()) {
512 EncoderProperties h264HighProfileProperties = findHwEncoder(H264_MIME_TYPE,
513 h264HighProfileHwList, useSurface ? supportedSurfaceColorList : supportedColorList);
514 if (h264HighProfileProperties != null) {
515 Logging.d(TAG, "High profile H.264 encoder supported.");
516 configureH264HighProfile = true;
517 } else {
518 Logging.d(TAG, "High profile H.264 encoder requested, but not supported. Use baseline.");
519 }
520 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000521 keyFrameIntervalSec = 20;
Niels Möller520ca4e2018-06-04 11:14:38 +0200522 } else {
523 throw new RuntimeException("initEncode: Non-supported codec " + type);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000524 }
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000525 if (properties == null) {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000526 throw new RuntimeException("Can not find HW encoder for " + type);
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000527 }
Alex Glaznevc6aec4b2015-10-19 16:39:19 -0700528 runningInstance = this; // Encoder is now running and can be queried for stack traces.
perkj9576e542015-11-12 06:43:16 -0800529 colorFormat = properties.colorFormat;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700530 bitrateAdjustmentType = properties.bitrateAdjustmentType;
531 if (bitrateAdjustmentType == BitrateAdjustmentType.FRAMERATE_ADJUSTMENT) {
Alex Glaznev269fe752016-05-25 16:17:33 -0700532 fps = BITRATE_ADJUSTMENT_FPS;
sakalb6760f92016-09-29 04:12:44 -0700533 } else {
Alex Glaznevc55c39d2016-09-02 12:16:27 -0700534 fps = Math.min(fps, MAXIMUM_INITIAL_FPS);
Alex Glaznev269fe752016-05-25 16:17:33 -0700535 }
Alex Glaznevc7483a72017-01-05 15:22:24 -0800536
537 forcedKeyFrameMs = 0;
538 lastKeyFrameMs = -1;
Alex Glaznev512f00b2017-01-05 16:40:46 -0800539 if (type == VideoCodecType.VIDEO_CODEC_VP8
540 && properties.codecName.startsWith(qcomVp8HwProperties.codecPrefix)) {
alexlau84ee5c62017-06-02 17:36:32 -0700541 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
542 || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
543 forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_L_MS;
544 } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
Alex Glaznevc7483a72017-01-05 15:22:24 -0800545 forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS;
546 } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
547 forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS;
548 }
549 }
550
sakalb6760f92016-09-29 04:12:44 -0700551 Logging.d(TAG, "Color format: " + colorFormat + ". Bitrate adjustment: " + bitrateAdjustmentType
Alex Glaznevc7483a72017-01-05 15:22:24 -0800552 + ". Key frame interval: " + forcedKeyFrameMs + " . Initial fps: " + fps);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700553 targetBitrateBps = 1000 * kbps;
554 targetFps = fps;
555 bitrateAccumulatorMax = targetBitrateBps / 8.0;
556 bitrateAccumulator = 0;
557 bitrateObservationTimeMs = 0;
558 bitrateAdjustmentScaleExp = 0;
perkj9576e542015-11-12 06:43:16 -0800559
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000560 mediaCodecThread = Thread.currentThread();
561 try {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000562 MediaFormat format = MediaFormat.createVideoFormat(mime, width, height);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700563 format.setInteger(MediaFormat.KEY_BIT_RATE, targetBitrateBps);
Alex Glaznev8a2cd3d2015-08-11 11:32:53 -0700564 format.setInteger("bitrate-mode", VIDEO_ControlRateConstant);
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000565 format.setInteger(MediaFormat.KEY_COLOR_FORMAT, properties.colorFormat);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700566 format.setInteger(MediaFormat.KEY_FRAME_RATE, targetFps);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000567 format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, keyFrameIntervalSec);
glaznev3fc23502017-06-15 16:24:37 -0700568 if (configureH264HighProfile) {
569 format.setInteger("profile", VIDEO_AVCProfileHigh);
570 format.setInteger("level", VIDEO_AVCLevel3);
571 }
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700572 Logging.d(TAG, " Format: " + format);
henrike@webrtc.org528fc652014-10-06 17:56:43 +0000573 mediaCodec = createByCodecName(properties.codecName);
perkj9576e542015-11-12 06:43:16 -0800574 this.type = type;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000575 if (mediaCodec == null) {
Alex Glaznev325d4142015-10-12 14:56:02 -0700576 Logging.e(TAG, "Can not create media encoder");
sakal996a83c2017-03-15 05:53:14 -0700577 release();
perkj9576e542015-11-12 06:43:16 -0800578 return false;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000579 }
sakalb6760f92016-09-29 04:12:44 -0700580 mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
perkj9576e542015-11-12 06:43:16 -0800581
perkj30e91822015-11-20 01:31:25 -0800582 if (useSurface) {
Magnus Jedvert0f0e7a62018-07-11 14:53:21 +0200583 eglBase = new EglBase14((EglBase14.Context) getEglContext(), EglBase.CONFIG_RECORDABLE);
perkj30e91822015-11-20 01:31:25 -0800584 // Create an input surface and keep a reference since we must release the surface when done.
585 inputSurface = mediaCodec.createInputSurface();
586 eglBase.createSurface(inputSurface);
587 drawer = new GlRectDrawer();
588 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000589 mediaCodec.start();
590 outputBuffers = mediaCodec.getOutputBuffers();
perkj9576e542015-11-12 06:43:16 -0800591 Logging.d(TAG, "Output buffers: " + outputBuffers.length);
592
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000593 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700594 Logging.e(TAG, "initEncode failed", e);
sakal996a83c2017-03-15 05:53:14 -0700595 release();
perkj9576e542015-11-12 06:43:16 -0800596 return false;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000597 }
perkj9576e542015-11-12 06:43:16 -0800598 return true;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000599 }
600
Magnus Jedvert655e1962017-12-08 11:05:22 +0100601 @CalledByNativeUnchecked
sakalb6760f92016-09-29 04:12:44 -0700602 ByteBuffer[] getInputBuffers() {
perkj9576e542015-11-12 06:43:16 -0800603 ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
604 Logging.d(TAG, "Input buffers: " + inputBuffers.length);
605 return inputBuffers;
606 }
607
Alex Glaznevc7483a72017-01-05 15:22:24 -0800608 void checkKeyFrameRequired(boolean requestedKeyFrame, long presentationTimestampUs) {
609 long presentationTimestampMs = (presentationTimestampUs + 500) / 1000;
610 if (lastKeyFrameMs < 0) {
611 lastKeyFrameMs = presentationTimestampMs;
612 }
613 boolean forcedKeyFrame = false;
614 if (!requestedKeyFrame && forcedKeyFrameMs > 0
615 && presentationTimestampMs > lastKeyFrameMs + forcedKeyFrameMs) {
616 forcedKeyFrame = true;
617 }
618 if (requestedKeyFrame || forcedKeyFrame) {
619 // Ideally MediaCodec would honor BUFFER_FLAG_SYNC_FRAME so we could
620 // indicate this in queueInputBuffer() below and guarantee _this_ frame
621 // be encoded as a key frame, but sadly that flag is ignored. Instead,
622 // we request a key frame "soon".
623 if (requestedKeyFrame) {
624 Logging.d(TAG, "Sync frame request");
625 } else {
626 Logging.d(TAG, "Sync frame forced");
627 }
628 Bundle b = new Bundle();
629 b.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
630 mediaCodec.setParameters(b);
631 lastKeyFrameMs = presentationTimestampMs;
632 }
633 }
634
Magnus Jedvert655e1962017-12-08 11:05:22 +0100635 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800636 boolean encodeBuffer(
sakalb6760f92016-09-29 04:12:44 -0700637 boolean isKeyframe, int inputBuffer, int size, long presentationTimestampUs) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000638 checkOnMediaCodecThread();
639 try {
Alex Glaznevc7483a72017-01-05 15:22:24 -0800640 checkKeyFrameRequired(isKeyframe, presentationTimestampUs);
sakalb6760f92016-09-29 04:12:44 -0700641 mediaCodec.queueInputBuffer(inputBuffer, 0, size, presentationTimestampUs, 0);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000642 return true;
sakalb6760f92016-09-29 04:12:44 -0700643 } catch (IllegalStateException e) {
perkj9576e542015-11-12 06:43:16 -0800644 Logging.e(TAG, "encodeBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000645 return false;
646 }
647 }
648
sakalb5f5bdc2017-08-10 04:15:42 -0700649 /**
Magnus Jedvert655e1962017-12-08 11:05:22 +0100650 * Encodes a new style VideoFrame. |bufferIndex| is -1 if we are not encoding in surface mode.
sakalb5f5bdc2017-08-10 04:15:42 -0700651 */
Magnus Jedvert655e1962017-12-08 11:05:22 +0100652 @CalledByNativeUnchecked
Sami Kalliomäkidebbc782018-02-02 10:01:46 +0100653 boolean encodeFrame(long nativeEncoder, boolean isKeyframe, VideoFrame frame, int bufferIndex,
654 long presentationTimestampUs) {
sakalb5f5bdc2017-08-10 04:15:42 -0700655 checkOnMediaCodecThread();
656 try {
sakalb5f5bdc2017-08-10 04:15:42 -0700657 checkKeyFrameRequired(isKeyframe, presentationTimestampUs);
658
659 VideoFrame.Buffer buffer = frame.getBuffer();
660 if (buffer instanceof VideoFrame.TextureBuffer) {
661 VideoFrame.TextureBuffer textureBuffer = (VideoFrame.TextureBuffer) buffer;
662 eglBase.makeCurrent();
663 // TODO(perkj): glClear() shouldn't be necessary since every pixel is covered anyway,
664 // but it's a workaround for bug webrtc:5147.
665 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
magjed7cede372017-09-11 06:12:07 -0700666 VideoFrameDrawer.drawTexture(drawer, textureBuffer, new Matrix() /* renderMatrix */, width,
sakal9bc599f2017-09-08 04:46:33 -0700667 height, 0 /* viewportX */, 0 /* viewportY */, width, height);
Sami Kalliomäkidebbc782018-02-02 10:01:46 +0100668 eglBase.swapBuffers(TimeUnit.MICROSECONDS.toNanos(presentationTimestampUs));
sakalb5f5bdc2017-08-10 04:15:42 -0700669 } else {
670 VideoFrame.I420Buffer i420Buffer = buffer.toI420();
Sami Kalliomäkie3044fe2017-10-02 09:41:55 +0200671 final int chromaHeight = (height + 1) / 2;
672 final ByteBuffer dataY = i420Buffer.getDataY();
673 final ByteBuffer dataU = i420Buffer.getDataU();
674 final ByteBuffer dataV = i420Buffer.getDataV();
675 final int strideY = i420Buffer.getStrideY();
676 final int strideU = i420Buffer.getStrideU();
677 final int strideV = i420Buffer.getStrideV();
678 if (dataY.capacity() < strideY * height) {
679 throw new RuntimeException("Y-plane buffer size too small.");
680 }
681 if (dataU.capacity() < strideU * chromaHeight) {
682 throw new RuntimeException("U-plane buffer size too small.");
683 }
684 if (dataV.capacity() < strideV * chromaHeight) {
685 throw new RuntimeException("V-plane buffer size too small.");
686 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100687 nativeFillInputBuffer(
Sami Kalliomäkie3044fe2017-10-02 09:41:55 +0200688 nativeEncoder, bufferIndex, dataY, strideY, dataU, strideU, dataV, strideV);
sakalb5f5bdc2017-08-10 04:15:42 -0700689 i420Buffer.release();
690 // I420 consists of one full-resolution and two half-resolution planes.
691 // 1 + 1 / 4 + 1 / 4 = 3 / 2
692 int yuvSize = width * height * 3 / 2;
693 mediaCodec.queueInputBuffer(bufferIndex, 0, yuvSize, presentationTimestampUs, 0);
694 }
695 return true;
696 } catch (RuntimeException e) {
697 Logging.e(TAG, "encodeFrame failed", e);
698 return false;
699 }
700 }
701
Magnus Jedvert655e1962017-12-08 11:05:22 +0100702 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800703 void release() {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700704 Logging.d(TAG, "Java releaseEncoder");
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000705 checkOnMediaCodecThread();
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700706
sakal996a83c2017-03-15 05:53:14 -0700707 class CaughtException {
708 Exception e;
709 }
710 final CaughtException caughtException = new CaughtException();
711 boolean stopHung = false;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700712
sakal996a83c2017-03-15 05:53:14 -0700713 if (mediaCodec != null) {
714 // Run Mediacodec stop() and release() on separate thread since sometime
715 // Mediacodec.stop() may hang.
716 final CountDownLatch releaseDone = new CountDownLatch(1);
717
718 Runnable runMediaCodecRelease = new Runnable() {
719 @Override
720 public void run() {
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700721 Logging.d(TAG, "Java releaseEncoder on release thread");
sakal996a83c2017-03-15 05:53:14 -0700722 try {
723 mediaCodec.stop();
724 } catch (Exception e) {
725 Logging.e(TAG, "Media encoder stop failed", e);
726 }
727 try {
728 mediaCodec.release();
729 } catch (Exception e) {
730 Logging.e(TAG, "Media encoder release failed", e);
731 caughtException.e = e;
732 }
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700733 Logging.d(TAG, "Java releaseEncoder on release thread done");
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700734
sakal996a83c2017-03-15 05:53:14 -0700735 releaseDone.countDown();
736 }
737 };
738 new Thread(runMediaCodecRelease).start();
739
740 if (!ThreadUtils.awaitUninterruptibly(releaseDone, MEDIA_CODEC_RELEASE_TIMEOUT_MS)) {
741 Logging.e(TAG, "Media encoder release timeout");
742 stopHung = true;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700743 }
sakal996a83c2017-03-15 05:53:14 -0700744
745 mediaCodec = null;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000746 }
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700747
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000748 mediaCodecThread = null;
perkj30e91822015-11-20 01:31:25 -0800749 if (drawer != null) {
750 drawer.release();
751 drawer = null;
752 }
753 if (eglBase != null) {
754 eglBase.release();
755 eglBase = null;
756 }
757 if (inputSurface != null) {
758 inputSurface.release();
759 inputSurface = null;
760 }
Alex Glaznevc6aec4b2015-10-19 16:39:19 -0700761 runningInstance = null;
sakal996a83c2017-03-15 05:53:14 -0700762
763 if (stopHung) {
764 codecErrors++;
765 if (errorCallback != null) {
766 Logging.e(TAG, "Invoke codec error callback. Errors: " + codecErrors);
767 errorCallback.onMediaCodecVideoEncoderCriticalError(codecErrors);
768 }
769 throw new RuntimeException("Media encoder release timeout.");
770 }
771
772 // Re-throw any runtime exception caught inside the other thread. Since this is an invoke, add
773 // stack trace for the waiting thread as well.
774 if (caughtException.e != null) {
775 final RuntimeException runtimeException = new RuntimeException(caughtException.e);
776 runtimeException.setStackTrace(ThreadUtils.concatStackTraces(
777 caughtException.e.getStackTrace(), runtimeException.getStackTrace()));
778 throw runtimeException;
779 }
780
Alex Glaznev325d4142015-10-12 14:56:02 -0700781 Logging.d(TAG, "Java releaseEncoder done");
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000782 }
783
Magnus Jedvert655e1962017-12-08 11:05:22 +0100784 @CalledByNativeUnchecked
Alex Glaznev269fe752016-05-25 16:17:33 -0700785 private boolean setRates(int kbps, int frameRate) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000786 checkOnMediaCodecThread();
Alex Glaznevcfaca032016-09-06 14:08:19 -0700787
788 int codecBitrateBps = 1000 * kbps;
789 if (bitrateAdjustmentType == BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) {
790 bitrateAccumulatorMax = codecBitrateBps / 8.0;
791 if (targetBitrateBps > 0 && codecBitrateBps < targetBitrateBps) {
792 // Rescale the accumulator level if the accumulator max decreases
793 bitrateAccumulator = bitrateAccumulator * codecBitrateBps / targetBitrateBps;
794 }
Alex Glaznev269fe752016-05-25 16:17:33 -0700795 }
Alex Glaznevcfaca032016-09-06 14:08:19 -0700796 targetBitrateBps = codecBitrateBps;
797 targetFps = frameRate;
798
799 // Adjust actual encoder bitrate based on bitrate adjustment type.
800 if (bitrateAdjustmentType == BitrateAdjustmentType.FRAMERATE_ADJUSTMENT && targetFps > 0) {
801 codecBitrateBps = BITRATE_ADJUSTMENT_FPS * targetBitrateBps / targetFps;
sakalb6760f92016-09-29 04:12:44 -0700802 Logging.v(TAG,
803 "setRates: " + kbps + " -> " + (codecBitrateBps / 1000) + " kbps. Fps: " + targetFps);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700804 } else if (bitrateAdjustmentType == BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) {
sakalb6760f92016-09-29 04:12:44 -0700805 Logging.v(TAG, "setRates: " + kbps + " kbps. Fps: " + targetFps + ". ExpScale: "
806 + bitrateAdjustmentScaleExp);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700807 if (bitrateAdjustmentScaleExp != 0) {
sakalb6760f92016-09-29 04:12:44 -0700808 codecBitrateBps = (int) (codecBitrateBps * getBitrateScale(bitrateAdjustmentScaleExp));
Alex Glaznevcfaca032016-09-06 14:08:19 -0700809 }
810 } else {
811 Logging.v(TAG, "setRates: " + kbps + " kbps. Fps: " + targetFps);
812 }
813
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000814 try {
815 Bundle params = new Bundle();
Alex Glaznevcfaca032016-09-06 14:08:19 -0700816 params.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, codecBitrateBps);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000817 mediaCodec.setParameters(params);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000818 return true;
819 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700820 Logging.e(TAG, "setRates failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000821 return false;
822 }
823 }
824
825 // Dequeue an input buffer and return its index, -1 if no input buffer is
826 // available, or -2 if the codec is no longer operative.
Magnus Jedvert655e1962017-12-08 11:05:22 +0100827 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800828 int dequeueInputBuffer() {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000829 checkOnMediaCodecThread();
830 try {
831 return mediaCodec.dequeueInputBuffer(DEQUEUE_TIMEOUT);
832 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700833 Logging.e(TAG, "dequeueIntputBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000834 return -2;
835 }
836 }
837
838 // Helper struct for dequeueOutputBuffer() below.
perkj9576e542015-11-12 06:43:16 -0800839 static class OutputBufferInfo {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000840 public OutputBufferInfo(
sakalb6760f92016-09-29 04:12:44 -0700841 int index, ByteBuffer buffer, boolean isKeyFrame, long presentationTimestampUs) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000842 this.index = index;
843 this.buffer = buffer;
844 this.isKeyFrame = isKeyFrame;
845 this.presentationTimestampUs = presentationTimestampUs;
846 }
847
perkj9576e542015-11-12 06:43:16 -0800848 public final int index;
849 public final ByteBuffer buffer;
850 public final boolean isKeyFrame;
851 public final long presentationTimestampUs;
Magnus Jedvert655e1962017-12-08 11:05:22 +0100852
853 @CalledByNative("OutputBufferInfo")
854 int getIndex() {
855 return index;
856 }
857
858 @CalledByNative("OutputBufferInfo")
859 ByteBuffer getBuffer() {
860 return buffer;
861 }
862
863 @CalledByNative("OutputBufferInfo")
864 boolean isKeyFrame() {
865 return isKeyFrame;
866 }
867
868 @CalledByNative("OutputBufferInfo")
869 long getPresentationTimestampUs() {
870 return presentationTimestampUs;
871 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000872 }
873
874 // Dequeue and return an output buffer, or null if no output is ready. Return
875 // a fake OutputBufferInfo with index -1 if the codec is no longer operable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100876 @Nullable
Magnus Jedvert655e1962017-12-08 11:05:22 +0100877 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800878 OutputBufferInfo dequeueOutputBuffer() {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000879 checkOnMediaCodecThread();
880 try {
881 MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
882 int result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000883 // Check if this is config frame and save configuration data.
884 if (result >= 0) {
sakalb6760f92016-09-29 04:12:44 -0700885 boolean isConfigFrame = (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000886 if (isConfigFrame) {
sakalb6760f92016-09-29 04:12:44 -0700887 Logging.d(TAG, "Config frame generated. Offset: " + info.offset + ". Size: " + info.size);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000888 configData = ByteBuffer.allocateDirect(info.size);
889 outputBuffers[result].position(info.offset);
890 outputBuffers[result].limit(info.offset + info.size);
891 configData.put(outputBuffers[result]);
glaznev3fc23502017-06-15 16:24:37 -0700892 // Log few SPS header bytes to check profile and level.
893 String spsData = "";
894 for (int i = 0; i < (info.size < 8 ? info.size : 8); i++) {
895 spsData += Integer.toHexString(configData.get(i) & 0xff) + " ";
896 }
897 Logging.d(TAG, spsData);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000898 // Release buffer back.
899 mediaCodec.releaseOutputBuffer(result, false);
900 // Query next output.
901 result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
902 }
903 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000904 if (result >= 0) {
905 // MediaCodec doesn't care about Buffer position/remaining/etc so we can
906 // mess with them to get a slice and avoid having to pass extra
907 // (BufferInfo-related) parameters back to C++.
908 ByteBuffer outputBuffer = outputBuffers[result].duplicate();
909 outputBuffer.position(info.offset);
910 outputBuffer.limit(info.offset + info.size);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700911 reportEncodedFrame(info.size);
912
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000913 // Check key frame flag.
sakalb6760f92016-09-29 04:12:44 -0700914 boolean isKeyFrame = (info.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
glaznev@webrtc.orgc6c1dfd2014-06-13 22:59:08 +0000915 if (isKeyFrame) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700916 Logging.d(TAG, "Sync frame generated");
glaznev@webrtc.orgc6c1dfd2014-06-13 22:59:08 +0000917 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000918 if (isKeyFrame && type == VideoCodecType.VIDEO_CODEC_H264) {
sakalb6760f92016-09-29 04:12:44 -0700919 Logging.d(TAG, "Appending config frame of size " + configData.capacity()
920 + " to output buffer with offset " + info.offset + ", size " + info.size);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000921 // For H.264 key frame append SPS and PPS NALs at the start
sakalb6760f92016-09-29 04:12:44 -0700922 ByteBuffer keyFrameBuffer = ByteBuffer.allocateDirect(configData.capacity() + info.size);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000923 configData.rewind();
924 keyFrameBuffer.put(configData);
925 keyFrameBuffer.put(outputBuffer);
926 keyFrameBuffer.position(0);
sakalb6760f92016-09-29 04:12:44 -0700927 return new OutputBufferInfo(result, keyFrameBuffer, isKeyFrame, info.presentationTimeUs);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000928 } else {
sakalb6760f92016-09-29 04:12:44 -0700929 return new OutputBufferInfo(
930 result, outputBuffer.slice(), isKeyFrame, info.presentationTimeUs);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000931 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000932 } else if (result == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
933 outputBuffers = mediaCodec.getOutputBuffers();
934 return dequeueOutputBuffer();
935 } else if (result == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
936 return dequeueOutputBuffer();
937 } else if (result == MediaCodec.INFO_TRY_AGAIN_LATER) {
938 return null;
939 }
940 throw new RuntimeException("dequeueOutputBuffer: " + result);
941 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700942 Logging.e(TAG, "dequeueOutputBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000943 return new OutputBufferInfo(-1, null, false, -1);
944 }
945 }
946
Alex Glaznevcfaca032016-09-06 14:08:19 -0700947 private double getBitrateScale(int bitrateAdjustmentScaleExp) {
948 return Math.pow(BITRATE_CORRECTION_MAX_SCALE,
sakalb6760f92016-09-29 04:12:44 -0700949 (double) bitrateAdjustmentScaleExp / BITRATE_CORRECTION_STEPS);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700950 }
951
952 private void reportEncodedFrame(int size) {
953 if (targetFps == 0 || bitrateAdjustmentType != BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) {
954 return;
955 }
956
957 // Accumulate the difference between actial and expected frame sizes.
958 double expectedBytesPerFrame = targetBitrateBps / (8.0 * targetFps);
959 bitrateAccumulator += (size - expectedBytesPerFrame);
960 bitrateObservationTimeMs += 1000.0 / targetFps;
961
962 // Put a cap on the accumulator, i.e., don't let it grow beyond some level to avoid
963 // using too old data for bitrate adjustment.
964 double bitrateAccumulatorCap = BITRATE_CORRECTION_SEC * bitrateAccumulatorMax;
965 bitrateAccumulator = Math.min(bitrateAccumulator, bitrateAccumulatorCap);
966 bitrateAccumulator = Math.max(bitrateAccumulator, -bitrateAccumulatorCap);
967
968 // Do bitrate adjustment every 3 seconds if actual encoder bitrate deviates too much
969 // form the target value.
970 if (bitrateObservationTimeMs > 1000 * BITRATE_CORRECTION_SEC) {
sakalb6760f92016-09-29 04:12:44 -0700971 Logging.d(TAG, "Acc: " + (int) bitrateAccumulator + ". Max: " + (int) bitrateAccumulatorMax
972 + ". ExpScale: " + bitrateAdjustmentScaleExp);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700973 boolean bitrateAdjustmentScaleChanged = false;
974 if (bitrateAccumulator > bitrateAccumulatorMax) {
975 // Encoder generates too high bitrate - need to reduce the scale.
Alex Glaznev7fa4a722017-01-17 15:32:02 -0800976 int bitrateAdjustmentInc = (int) (bitrateAccumulator / bitrateAccumulatorMax + 0.5);
977 bitrateAdjustmentScaleExp -= bitrateAdjustmentInc;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700978 bitrateAccumulator = bitrateAccumulatorMax;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700979 bitrateAdjustmentScaleChanged = true;
980 } else if (bitrateAccumulator < -bitrateAccumulatorMax) {
981 // Encoder generates too low bitrate - need to increase the scale.
Alex Glaznev7fa4a722017-01-17 15:32:02 -0800982 int bitrateAdjustmentInc = (int) (-bitrateAccumulator / bitrateAccumulatorMax + 0.5);
983 bitrateAdjustmentScaleExp += bitrateAdjustmentInc;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700984 bitrateAccumulator = -bitrateAccumulatorMax;
985 bitrateAdjustmentScaleChanged = true;
986 }
987 if (bitrateAdjustmentScaleChanged) {
988 bitrateAdjustmentScaleExp = Math.min(bitrateAdjustmentScaleExp, BITRATE_CORRECTION_STEPS);
989 bitrateAdjustmentScaleExp = Math.max(bitrateAdjustmentScaleExp, -BITRATE_CORRECTION_STEPS);
sakalb6760f92016-09-29 04:12:44 -0700990 Logging.d(TAG, "Adjusting bitrate scale to " + bitrateAdjustmentScaleExp + ". Value: "
991 + getBitrateScale(bitrateAdjustmentScaleExp));
Alex Glaznevcfaca032016-09-06 14:08:19 -0700992 setRates(targetBitrateBps / 1000, targetFps);
993 }
994 bitrateObservationTimeMs = 0;
995 }
996 }
997
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000998 // Release a dequeued output buffer back to the codec for re-use. Return
999 // false if the codec is no longer operable.
Magnus Jedvert655e1962017-12-08 11:05:22 +01001000 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -08001001 boolean releaseOutputBuffer(int index) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +00001002 checkOnMediaCodecThread();
1003 try {
1004 mediaCodec.releaseOutputBuffer(index, false);
1005 return true;
1006 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -07001007 Logging.e(TAG, "releaseOutputBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +00001008 return false;
1009 }
1010 }
sakalb5f5bdc2017-08-10 04:15:42 -07001011
Magnus Jedvert655e1962017-12-08 11:05:22 +01001012 @CalledByNative
1013 int getColorFormat() {
1014 return colorFormat;
1015 }
1016
1017 @CalledByNative
1018 static boolean isTextureBuffer(VideoFrame.Buffer buffer) {
1019 return buffer instanceof VideoFrame.TextureBuffer;
1020 }
1021
sakalb5f5bdc2017-08-10 04:15:42 -07001022 /** Fills an inputBuffer with the given index with data from the byte buffers. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001023 private static native void nativeFillInputBuffer(long encoder, int inputBuffer, ByteBuffer dataY,
1024 int strideY, ByteBuffer dataU, int strideU, ByteBuffer dataV, int strideV);
fischman@webrtc.org540acde2014-02-13 03:56:14 +00001025}