fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 11 | package org.webrtc; |
| 12 | |
Patrik Höglund | 68876f9 | 2015-11-12 17:36:48 +0100 | [diff] [blame] | 13 | import android.annotation.TargetApi; |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 14 | import android.graphics.Matrix; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 15 | import android.media.MediaCodec; |
| 16 | import android.media.MediaCodecInfo; |
Sami Kalliomaki | d3235f0 | 2016-08-02 15:44:04 +0200 | [diff] [blame] | 17 | import android.media.MediaCodecInfo.CodecCapabilities; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 18 | import android.media.MediaCodecList; |
| 19 | import android.media.MediaFormat; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 20 | import android.opengl.GLES20; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 21 | import android.os.Build; |
| 22 | import android.os.Bundle; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 23 | import android.view.Surface; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 24 | import java.nio.ByteBuffer; |
magjed | 295760d | 2017-01-12 01:11:57 -0800 | [diff] [blame] | 25 | import java.util.ArrayList; |
Alex Glaznev | 0c85020 | 2015-08-04 10:26:59 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 27 | import java.util.HashSet; |
Alex Glaznev | 0c85020 | 2015-08-04 10:26:59 -0700 | [diff] [blame] | 28 | import java.util.List; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 29 | import java.util.Set; |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 30 | import java.util.concurrent.CountDownLatch; |
perkj | 48477c1 | 2015-12-18 00:34:37 -0800 | [diff] [blame] | 31 | import java.util.concurrent.TimeUnit; |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 32 | import org.webrtc.EglBase14; |
| 33 | import org.webrtc.VideoFrame; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 34 | |
Magnus Jedvert | 9060eb1 | 2017-12-12 12:52:54 +0100 | [diff] [blame] | 35 | // Java-side of peerconnection.cc:MediaCodecVideoEncoder. |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 36 | // This class is an implementation detail of the Java PeerConnection API. |
Patrik Höglund | 68876f9 | 2015-11-12 17:36:48 +0100 | [diff] [blame] | 37 | @TargetApi(19) |
| 38 | @SuppressWarnings("deprecation") |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 39 | @JNINamespace("webrtc::jni") |
perkj@webrtc.org | 4709887 | 2014-10-24 11:38:19 +0000 | [diff] [blame] | 40 | public class MediaCodecVideoEncoder { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 41 | // This class is constructed, operated, and destroyed by its C++ incarnation, |
| 42 | // so the class and its methods have non-public visibility. The API this |
| 43 | // class exposes aims to mimic the webrtc::VideoEncoder API as closely as |
| 44 | // possibly to minimize the amount of translation work necessary. |
| 45 | |
| 46 | private static final String TAG = "MediaCodecVideoEncoder"; |
| 47 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 48 | // Tracks webrtc::VideoCodecType. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 49 | public enum VideoCodecType { |
| 50 | VIDEO_CODEC_VP8, |
| 51 | VIDEO_CODEC_VP9, |
| 52 | VIDEO_CODEC_H264; |
| 53 | |
| 54 | @CalledByNative("VideoCodecType") |
| 55 | static VideoCodecType fromNativeIndex(int nativeIndex) { |
| 56 | return values()[nativeIndex]; |
| 57 | } |
| 58 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 59 | |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 60 | private static final int MEDIA_CODEC_RELEASE_TIMEOUT_MS = 5000; // Timeout for codec releasing. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 61 | private static final int DEQUEUE_TIMEOUT = 0; // Non-blocking, no wait. |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 62 | private static final int BITRATE_ADJUSTMENT_FPS = 30; |
Alex Glaznev | c55c39d | 2016-09-02 12:16:27 -0700 | [diff] [blame] | 63 | private static final int MAXIMUM_INITIAL_FPS = 30; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 64 | private static final double BITRATE_CORRECTION_SEC = 3.0; |
Alex Glaznev | 7fa4a72 | 2017-01-17 15:32:02 -0800 | [diff] [blame] | 65 | // Maximum bitrate correction scale - no more than 4 times. |
| 66 | private static final double BITRATE_CORRECTION_MAX_SCALE = 4; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 67 | // Amount of correction steps to reach correction maximum scale. |
Alex Glaznev | 7fa4a72 | 2017-01-17 15:32:02 -0800 | [diff] [blame] | 68 | private static final int BITRATE_CORRECTION_STEPS = 20; |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 69 | // Forced key frame interval - used to reduce color distortions on Qualcomm platform. |
alexlau | 84ee5c6 | 2017-06-02 17:36:32 -0700 | [diff] [blame] | 70 | private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_L_MS = 15000; |
| 71 | private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS = 20000; |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 72 | private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS = 15000; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 73 | |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 74 | // Active running encoder instance. Set in initEncode() (called from native code) |
Alex Glaznev | c6aec4b | 2015-10-19 16:39:19 -0700 | [diff] [blame] | 75 | // and reset to null in release() call. |
| 76 | private static MediaCodecVideoEncoder runningInstance = null; |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 77 | private static MediaCodecVideoEncoderErrorCallback errorCallback = null; |
| 78 | private static int codecErrors = 0; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 79 | // List of disabled codec types - can be set from application. |
| 80 | private static Set<String> hwEncoderDisabledTypes = new HashSet<String>(); |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 81 | |
Alejandro Luebs | 69ddaef | 2015-10-09 15:46:09 -0700 | [diff] [blame] | 82 | private Thread mediaCodecThread; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 83 | private MediaCodec mediaCodec; |
| 84 | private ByteBuffer[] outputBuffers; |
perkj | 48477c1 | 2015-12-18 00:34:37 -0800 | [diff] [blame] | 85 | private EglBase14 eglBase; |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 86 | private int profile; |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 87 | private int width; |
| 88 | private int height; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 89 | private Surface inputSurface; |
| 90 | private GlRectDrawer drawer; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 91 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 92 | private static final String VP8_MIME_TYPE = "video/x-vnd.on2.vp8"; |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 93 | private static final String VP9_MIME_TYPE = "video/x-vnd.on2.vp9"; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 94 | private static final String H264_MIME_TYPE = "video/avc"; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 95 | |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 96 | private static final int VIDEO_AVCProfileHigh = 8; |
| 97 | private static final int VIDEO_AVCLevel3 = 0x100; |
| 98 | |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 99 | // Type of bitrate adjustment for video encoder. |
| 100 | public enum BitrateAdjustmentType { |
| 101 | // No adjustment - video encoder has no known bitrate problem. |
| 102 | NO_ADJUSTMENT, |
| 103 | // Framerate based bitrate adjustment is required - HW encoder does not use frame |
| 104 | // timestamps to calculate frame bitrate budget and instead is relying on initial |
| 105 | // fps configuration assuming that all frames are coming at fixed initial frame rate. |
| 106 | FRAMERATE_ADJUSTMENT, |
| 107 | // Dynamic bitrate adjustment is required - HW encoder used frame timestamps, but actual |
| 108 | // bitrate deviates too much from the target value. |
| 109 | DYNAMIC_ADJUSTMENT |
| 110 | } |
| 111 | |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 112 | // Should be in sync with webrtc::H264::Profile. |
| 113 | public static enum H264Profile { |
| 114 | CONSTRAINED_BASELINE(0), |
| 115 | BASELINE(1), |
| 116 | MAIN(2), |
| 117 | CONSTRAINED_HIGH(3), |
| 118 | HIGH(4); |
| 119 | |
| 120 | private final int value; |
| 121 | |
| 122 | H264Profile(int value) { |
| 123 | this.value = value; |
| 124 | } |
| 125 | |
| 126 | public int getValue() { |
| 127 | return value; |
| 128 | } |
| 129 | } |
| 130 | |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 131 | // Class describing supported media codec properties. |
| 132 | private static class MediaCodecProperties { |
| 133 | public final String codecPrefix; |
| 134 | // Minimum Android SDK required for this codec to be used. |
| 135 | public final int minSdk; |
| 136 | // Flag if encoder implementation does not use frame timestamps to calculate frame bitrate |
| 137 | // budget and instead is relying on initial fps configuration assuming that all frames are |
| 138 | // coming at fixed initial frame rate. Bitrate adjustment is required for this case. |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 139 | public final BitrateAdjustmentType bitrateAdjustmentType; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 140 | |
| 141 | MediaCodecProperties( |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 142 | String codecPrefix, int minSdk, BitrateAdjustmentType bitrateAdjustmentType) { |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 143 | this.codecPrefix = codecPrefix; |
| 144 | this.minSdk = minSdk; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 145 | this.bitrateAdjustmentType = bitrateAdjustmentType; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Alex Glaznev | dcd730f | 2016-04-21 17:01:46 -0700 | [diff] [blame] | 149 | // List of supported HW VP8 encoders. |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 150 | private static final MediaCodecProperties qcomVp8HwProperties = new MediaCodecProperties( |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 151 | "OMX.qcom.", Build.VERSION_CODES.KITKAT, BitrateAdjustmentType.NO_ADJUSTMENT); |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 152 | private static final MediaCodecProperties exynosVp8HwProperties = new MediaCodecProperties( |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 153 | "OMX.Exynos.", Build.VERSION_CODES.M, BitrateAdjustmentType.DYNAMIC_ADJUSTMENT); |
magjed | 295760d | 2017-01-12 01:11:57 -0800 | [diff] [blame] | 154 | private static final MediaCodecProperties intelVp8HwProperties = new MediaCodecProperties( |
| 155 | "OMX.Intel.", Build.VERSION_CODES.LOLLIPOP, BitrateAdjustmentType.NO_ADJUSTMENT); |
| 156 | private static MediaCodecProperties[] vp8HwList() { |
| 157 | final ArrayList<MediaCodecProperties> supported_codecs = new ArrayList<MediaCodecProperties>(); |
| 158 | supported_codecs.add(qcomVp8HwProperties); |
| 159 | supported_codecs.add(exynosVp8HwProperties); |
| 160 | if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-IntelVP8").equals("Enabled")) { |
| 161 | supported_codecs.add(intelVp8HwProperties); |
| 162 | } |
| 163 | return supported_codecs.toArray(new MediaCodecProperties[supported_codecs.size()]); |
| 164 | } |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 165 | |
Alex Glaznev | dcd730f | 2016-04-21 17:01:46 -0700 | [diff] [blame] | 166 | // List of supported HW VP9 encoders. |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 167 | private static final MediaCodecProperties qcomVp9HwProperties = new MediaCodecProperties( |
glaznev | 6fac429 | 2017-06-02 20:18:54 -0700 | [diff] [blame] | 168 | "OMX.qcom.", Build.VERSION_CODES.N, BitrateAdjustmentType.NO_ADJUSTMENT); |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 169 | private static final MediaCodecProperties exynosVp9HwProperties = new MediaCodecProperties( |
glaznev | 6fac429 | 2017-06-02 20:18:54 -0700 | [diff] [blame] | 170 | "OMX.Exynos.", Build.VERSION_CODES.N, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 171 | private static final MediaCodecProperties[] vp9HwList = |
| 172 | new MediaCodecProperties[] {qcomVp9HwProperties, exynosVp9HwProperties}; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 173 | |
Alex Glaznev | dcd730f | 2016-04-21 17:01:46 -0700 | [diff] [blame] | 174 | // List of supported HW H.264 encoders. |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 175 | private static final MediaCodecProperties qcomH264HwProperties = new MediaCodecProperties( |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 176 | "OMX.qcom.", Build.VERSION_CODES.KITKAT, BitrateAdjustmentType.NO_ADJUSTMENT); |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 177 | private static final MediaCodecProperties exynosH264HwProperties = new MediaCodecProperties( |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 178 | "OMX.Exynos.", Build.VERSION_CODES.LOLLIPOP, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT); |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 179 | private static final MediaCodecProperties mediatekH264HwProperties = new MediaCodecProperties( |
| 180 | "OMX.MTK.", Build.VERSION_CODES.O, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT); |
| 181 | private static final MediaCodecProperties[] h264HwList() { |
| 182 | final ArrayList<MediaCodecProperties> supported_codecs = new ArrayList<MediaCodecProperties>(); |
| 183 | supported_codecs.add(qcomH264HwProperties); |
| 184 | supported_codecs.add(exynosH264HwProperties); |
| 185 | if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-MediaTekH264").equals("Enabled")) { |
| 186 | supported_codecs.add(mediatekH264HwProperties); |
| 187 | } |
| 188 | return supported_codecs.toArray(new MediaCodecProperties[supported_codecs.size()]); |
| 189 | } |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 190 | |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 191 | // List of supported HW H.264 high profile encoders. |
| 192 | private static final MediaCodecProperties exynosH264HighProfileHwProperties = |
| 193 | new MediaCodecProperties( |
| 194 | "OMX.Exynos.", Build.VERSION_CODES.M, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT); |
| 195 | private static final MediaCodecProperties[] h264HighProfileHwList = |
| 196 | new MediaCodecProperties[] {exynosH264HighProfileHwProperties}; |
| 197 | |
Alex Glaznev | 0c85020 | 2015-08-04 10:26:59 -0700 | [diff] [blame] | 198 | // List of devices with poor H.264 encoder quality. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 199 | // HW H.264 encoder on below devices has poor bitrate control - actual |
| 200 | // bitrates deviates a lot from the target value. |
| 201 | private static final String[] H264_HW_EXCEPTION_MODELS = |
| 202 | new String[] {"SAMSUNG-SGH-I337", "Nexus 7", "Nexus 4"}; |
Alex Glaznev | 0c85020 | 2015-08-04 10:26:59 -0700 | [diff] [blame] | 203 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 204 | // Bitrate modes - should be in sync with OMX_VIDEO_CONTROLRATETYPE defined |
| 205 | // in OMX_Video.h |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 206 | private static final int VIDEO_ControlRateConstant = 2; |
| 207 | // NV12 color format supported by QCOM codec, but not declared in MediaCodec - |
| 208 | // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 209 | private static final int COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04; |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 210 | // Allowable color formats supported by codec - in order of preference. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 211 | private static final int[] supportedColorList = {CodecCapabilities.COLOR_FormatYUV420Planar, |
| 212 | CodecCapabilities.COLOR_FormatYUV420SemiPlanar, |
| 213 | CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar, |
| 214 | COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m}; |
| 215 | private static final int[] supportedSurfaceColorList = {CodecCapabilities.COLOR_FormatSurface}; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 216 | private VideoCodecType type; |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 217 | private int colorFormat; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 218 | |
| 219 | // Variables used for dynamic bitrate adjustment. |
| 220 | private BitrateAdjustmentType bitrateAdjustmentType = BitrateAdjustmentType.NO_ADJUSTMENT; |
| 221 | private double bitrateAccumulator; |
| 222 | private double bitrateAccumulatorMax; |
| 223 | private double bitrateObservationTimeMs; |
| 224 | private int bitrateAdjustmentScaleExp; |
| 225 | private int targetBitrateBps; |
| 226 | private int targetFps; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 227 | |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 228 | // Interval in ms to force key frame generation. Used to reduce the time of color distortions |
| 229 | // happened sometime when using Qualcomm video encoder. |
| 230 | private long forcedKeyFrameMs; |
| 231 | private long lastKeyFrameMs; |
| 232 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 233 | // SPS and PPS NALs (Config frame) for H.264. |
| 234 | private ByteBuffer configData = null; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 235 | |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 236 | // MediaCodec error handler - invoked when critical error happens which may prevent |
| 237 | // further use of media codec API. Now it means that one of media codec instances |
| 238 | // is hanging and can no longer be used in the next call. |
| 239 | public static interface MediaCodecVideoEncoderErrorCallback { |
| 240 | void onMediaCodecVideoEncoderCriticalError(int codecErrors); |
| 241 | } |
| 242 | |
| 243 | public static void setErrorCallback(MediaCodecVideoEncoderErrorCallback errorCallback) { |
| 244 | Logging.d(TAG, "Set error callback"); |
| 245 | MediaCodecVideoEncoder.errorCallback = errorCallback; |
| 246 | } |
| 247 | |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 248 | // Functions to disable HW encoding - can be called from applications for platforms |
| 249 | // which have known HW decoding problems. |
| 250 | public static void disableVp8HwCodec() { |
| 251 | Logging.w(TAG, "VP8 encoding is disabled by application."); |
| 252 | hwEncoderDisabledTypes.add(VP8_MIME_TYPE); |
| 253 | } |
| 254 | |
| 255 | public static void disableVp9HwCodec() { |
| 256 | Logging.w(TAG, "VP9 encoding is disabled by application."); |
| 257 | hwEncoderDisabledTypes.add(VP9_MIME_TYPE); |
| 258 | } |
| 259 | |
| 260 | public static void disableH264HwCodec() { |
| 261 | Logging.w(TAG, "H.264 encoding is disabled by application."); |
| 262 | hwEncoderDisabledTypes.add(H264_MIME_TYPE); |
| 263 | } |
| 264 | |
| 265 | // Functions to query if HW encoding is supported. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 266 | @CalledByNative |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 267 | public static boolean isVp8HwSupported() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 268 | return !hwEncoderDisabledTypes.contains(VP8_MIME_TYPE) |
magjed | 295760d | 2017-01-12 01:11:57 -0800 | [diff] [blame] | 269 | && (findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedColorList) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 272 | public static EncoderProperties vp8HwEncoderProperties() { |
| 273 | if (hwEncoderDisabledTypes.contains(VP8_MIME_TYPE)) { |
| 274 | return null; |
| 275 | } else { |
magjed | 295760d | 2017-01-12 01:11:57 -0800 | [diff] [blame] | 276 | return findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedColorList); |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 280 | @CalledByNative |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 281 | public static boolean isVp9HwSupported() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 282 | return !hwEncoderDisabledTypes.contains(VP9_MIME_TYPE) |
| 283 | && (findHwEncoder(VP9_MIME_TYPE, vp9HwList, supportedColorList) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 286 | @CalledByNative |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 287 | public static boolean isH264HwSupported() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 288 | return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE) |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 289 | && (findHwEncoder(H264_MIME_TYPE, h264HwList(), supportedColorList) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 290 | } |
| 291 | |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 292 | public static boolean isH264HighProfileHwSupported() { |
| 293 | return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE) |
| 294 | && (findHwEncoder(H264_MIME_TYPE, h264HighProfileHwList, supportedColorList) != null); |
| 295 | } |
| 296 | |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 297 | public static boolean isVp8HwSupportedUsingTextures() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 298 | return !hwEncoderDisabledTypes.contains(VP8_MIME_TYPE) |
magjed | 295760d | 2017-01-12 01:11:57 -0800 | [diff] [blame] | 299 | && (findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedSurfaceColorList) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | public static boolean isVp9HwSupportedUsingTextures() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 303 | return !hwEncoderDisabledTypes.contains(VP9_MIME_TYPE) |
| 304 | && (findHwEncoder(VP9_MIME_TYPE, vp9HwList, supportedSurfaceColorList) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | public static boolean isH264HwSupportedUsingTextures() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 308 | return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE) |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 309 | && (findHwEncoder(H264_MIME_TYPE, h264HwList(), supportedSurfaceColorList) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 310 | } |
| 311 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 312 | // Helper struct for findHwEncoder() below. |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 313 | public static class EncoderProperties { |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 314 | public EncoderProperties( |
| 315 | String codecName, int colorFormat, BitrateAdjustmentType bitrateAdjustmentType) { |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 316 | this.codecName = codecName; |
| 317 | this.colorFormat = colorFormat; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 318 | this.bitrateAdjustmentType = bitrateAdjustmentType; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 319 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 320 | public final String codecName; // OpenMax component name for HW codec. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 321 | public final int colorFormat; // Color format supported by codec. |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 322 | public final BitrateAdjustmentType bitrateAdjustmentType; // Bitrate adjustment type |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 323 | } |
| 324 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 325 | private static EncoderProperties findHwEncoder( |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 326 | String mime, MediaCodecProperties[] supportedHwCodecProperties, int[] colorList) { |
Alex Glaznev | 0c85020 | 2015-08-04 10:26:59 -0700 | [diff] [blame] | 327 | // MediaCodec.setParameters is missing for JB and below, so bitrate |
| 328 | // can not be adjusted dynamically. |
| 329 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { |
| 330 | return null; |
| 331 | } |
| 332 | |
| 333 | // Check if device is in H.264 exception list. |
| 334 | if (mime.equals(H264_MIME_TYPE)) { |
| 335 | List<String> exceptionModels = Arrays.asList(H264_HW_EXCEPTION_MODELS); |
| 336 | if (exceptionModels.contains(Build.MODEL)) { |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 337 | Logging.w(TAG, "Model: " + Build.MODEL + " has black listed H.264 encoder."); |
Alex Glaznev | 0c85020 | 2015-08-04 10:26:59 -0700 | [diff] [blame] | 338 | return null; |
| 339 | } |
| 340 | } |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 341 | |
| 342 | for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) { |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 343 | MediaCodecInfo info = null; |
| 344 | try { |
| 345 | info = MediaCodecList.getCodecInfoAt(i); |
| 346 | } catch (IllegalArgumentException e) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 347 | Logging.e(TAG, "Cannot retrieve encoder codec info", e); |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 348 | } |
| 349 | if (info == null || !info.isEncoder()) { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 350 | continue; |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 351 | } |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 352 | String name = null; |
| 353 | for (String mimeType : info.getSupportedTypes()) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 354 | if (mimeType.equals(mime)) { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 355 | name = info.getName(); |
| 356 | break; |
| 357 | } |
| 358 | } |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 359 | if (name == null) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 360 | continue; // No HW support in this codec; try the next one. |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 361 | } |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 362 | Logging.v(TAG, "Found candidate encoder " + name); |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 363 | |
| 364 | // Check if this is supported HW encoder. |
| 365 | boolean supportedCodec = false; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 366 | BitrateAdjustmentType bitrateAdjustmentType = BitrateAdjustmentType.NO_ADJUSTMENT; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 367 | for (MediaCodecProperties codecProperties : supportedHwCodecProperties) { |
| 368 | if (name.startsWith(codecProperties.codecPrefix)) { |
| 369 | if (Build.VERSION.SDK_INT < codecProperties.minSdk) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 370 | Logging.w( |
| 371 | TAG, "Codec " + name + " is disabled due to SDK version " + Build.VERSION.SDK_INT); |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 372 | continue; |
| 373 | } |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 374 | if (codecProperties.bitrateAdjustmentType != BitrateAdjustmentType.NO_ADJUSTMENT) { |
| 375 | bitrateAdjustmentType = codecProperties.bitrateAdjustmentType; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 376 | Logging.w( |
| 377 | TAG, "Codec " + name + " requires bitrate adjustment: " + bitrateAdjustmentType); |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 378 | } |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 379 | supportedCodec = true; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | if (!supportedCodec) { |
| 384 | continue; |
| 385 | } |
| 386 | |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 387 | // Check if HW codec supports known color format. |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 388 | CodecCapabilities capabilities; |
| 389 | try { |
| 390 | capabilities = info.getCapabilitiesForType(mime); |
| 391 | } catch (IllegalArgumentException e) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 392 | Logging.e(TAG, "Cannot retrieve encoder capabilities", e); |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 393 | continue; |
| 394 | } |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 395 | for (int colorFormat : capabilities.colorFormats) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 396 | Logging.v(TAG, " Color: 0x" + Integer.toHexString(colorFormat)); |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 397 | } |
| 398 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 399 | for (int supportedColorFormat : colorList) { |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 400 | for (int codecColorFormat : capabilities.colorFormats) { |
| 401 | if (codecColorFormat == supportedColorFormat) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 402 | // Found supported HW encoder. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 403 | Logging.d(TAG, "Found target encoder for mime " + mime + " : " + name + ". Color: 0x" |
| 404 | + Integer.toHexString(codecColorFormat) + ". Bitrate adjustment: " |
| 405 | + bitrateAdjustmentType); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 406 | return new EncoderProperties(name, codecColorFormat, bitrateAdjustmentType); |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 410 | } |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 411 | return null; // No HW encoder. |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 414 | @CalledByNative |
| 415 | MediaCodecVideoEncoder() {} |
| 416 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 417 | private void checkOnMediaCodecThread() { |
| 418 | if (mediaCodecThread.getId() != Thread.currentThread().getId()) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 419 | throw new RuntimeException("MediaCodecVideoEncoder previously operated on " + mediaCodecThread |
| 420 | + " but is now called on " + Thread.currentThread()); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Alex Glaznev | 325d414 | 2015-10-12 14:56:02 -0700 | [diff] [blame] | 424 | public static void printStackTrace() { |
Alex Glaznev | c6aec4b | 2015-10-19 16:39:19 -0700 | [diff] [blame] | 425 | if (runningInstance != null && runningInstance.mediaCodecThread != null) { |
| 426 | StackTraceElement[] mediaCodecStackTraces = runningInstance.mediaCodecThread.getStackTrace(); |
Alex Glaznev | 325d414 | 2015-10-12 14:56:02 -0700 | [diff] [blame] | 427 | if (mediaCodecStackTraces.length > 0) { |
| 428 | Logging.d(TAG, "MediaCodecVideoEncoder stacks trace:"); |
| 429 | for (StackTraceElement stackTrace : mediaCodecStackTraces) { |
| 430 | Logging.d(TAG, stackTrace.toString()); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
henrike@webrtc.org | 528fc65 | 2014-10-06 17:56:43 +0000 | [diff] [blame] | 436 | static MediaCodec createByCodecName(String codecName) { |
| 437 | try { |
| 438 | // In the L-SDK this call can throw IOException so in order to work in |
| 439 | // both cases catch an exception. |
| 440 | return MediaCodec.createByCodecName(codecName); |
| 441 | } catch (Exception e) { |
| 442 | return null; |
| 443 | } |
| 444 | } |
| 445 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 446 | @CalledByNativeUnchecked |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 447 | boolean initEncode(VideoCodecType type, int profile, int width, int height, int kbps, int fps, |
perkj | 48477c1 | 2015-12-18 00:34:37 -0800 | [diff] [blame] | 448 | EglBase14.Context sharedContext) { |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 449 | final boolean useSurface = sharedContext != null; |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 450 | Logging.d(TAG, |
| 451 | "Java initEncode: " + type + ". Profile: " + profile + " : " + width + " x " + height |
| 452 | + ". @ " + kbps + " kbps. Fps: " + fps + ". Encode from texture : " + useSurface); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 453 | |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 454 | this.profile = profile; |
Magnus Jedvert | 5125433 | 2015-12-15 16:22:29 +0100 | [diff] [blame] | 455 | this.width = width; |
| 456 | this.height = height; |
Alejandro Luebs | 69ddaef | 2015-10-09 15:46:09 -0700 | [diff] [blame] | 457 | if (mediaCodecThread != null) { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 458 | throw new RuntimeException("Forgot to release()?"); |
| 459 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 460 | EncoderProperties properties = null; |
| 461 | String mime = null; |
| 462 | int keyFrameIntervalSec = 0; |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 463 | boolean configureH264HighProfile = false; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 464 | if (type == VideoCodecType.VIDEO_CODEC_VP8) { |
| 465 | mime = VP8_MIME_TYPE; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 466 | properties = findHwEncoder( |
magjed | 295760d | 2017-01-12 01:11:57 -0800 | [diff] [blame] | 467 | VP8_MIME_TYPE, vp8HwList(), useSurface ? supportedSurfaceColorList : supportedColorList); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 468 | keyFrameIntervalSec = 100; |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 469 | } else if (type == VideoCodecType.VIDEO_CODEC_VP9) { |
| 470 | mime = VP9_MIME_TYPE; |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 471 | properties = findHwEncoder( |
| 472 | VP9_MIME_TYPE, vp9HwList, useSurface ? supportedSurfaceColorList : supportedColorList); |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 473 | keyFrameIntervalSec = 100; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 474 | } else if (type == VideoCodecType.VIDEO_CODEC_H264) { |
| 475 | mime = H264_MIME_TYPE; |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 476 | properties = findHwEncoder(H264_MIME_TYPE, h264HwList(), |
| 477 | useSurface ? supportedSurfaceColorList : supportedColorList); |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 478 | if (profile == H264Profile.CONSTRAINED_HIGH.getValue()) { |
| 479 | EncoderProperties h264HighProfileProperties = findHwEncoder(H264_MIME_TYPE, |
| 480 | h264HighProfileHwList, useSurface ? supportedSurfaceColorList : supportedColorList); |
| 481 | if (h264HighProfileProperties != null) { |
| 482 | Logging.d(TAG, "High profile H.264 encoder supported."); |
| 483 | configureH264HighProfile = true; |
| 484 | } else { |
| 485 | Logging.d(TAG, "High profile H.264 encoder requested, but not supported. Use baseline."); |
| 486 | } |
| 487 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 488 | keyFrameIntervalSec = 20; |
| 489 | } |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 490 | if (properties == null) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 491 | throw new RuntimeException("Can not find HW encoder for " + type); |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 492 | } |
Alex Glaznev | c6aec4b | 2015-10-19 16:39:19 -0700 | [diff] [blame] | 493 | runningInstance = this; // Encoder is now running and can be queried for stack traces. |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 494 | colorFormat = properties.colorFormat; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 495 | bitrateAdjustmentType = properties.bitrateAdjustmentType; |
| 496 | if (bitrateAdjustmentType == BitrateAdjustmentType.FRAMERATE_ADJUSTMENT) { |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 497 | fps = BITRATE_ADJUSTMENT_FPS; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 498 | } else { |
Alex Glaznev | c55c39d | 2016-09-02 12:16:27 -0700 | [diff] [blame] | 499 | fps = Math.min(fps, MAXIMUM_INITIAL_FPS); |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 500 | } |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 501 | |
| 502 | forcedKeyFrameMs = 0; |
| 503 | lastKeyFrameMs = -1; |
Alex Glaznev | 512f00b | 2017-01-05 16:40:46 -0800 | [diff] [blame] | 504 | if (type == VideoCodecType.VIDEO_CODEC_VP8 |
| 505 | && properties.codecName.startsWith(qcomVp8HwProperties.codecPrefix)) { |
alexlau | 84ee5c6 | 2017-06-02 17:36:32 -0700 | [diff] [blame] | 506 | if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP |
| 507 | || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 508 | forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_L_MS; |
| 509 | } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) { |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 510 | forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS; |
| 511 | } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { |
| 512 | forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS; |
| 513 | } |
| 514 | } |
| 515 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 516 | Logging.d(TAG, "Color format: " + colorFormat + ". Bitrate adjustment: " + bitrateAdjustmentType |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 517 | + ". Key frame interval: " + forcedKeyFrameMs + " . Initial fps: " + fps); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 518 | targetBitrateBps = 1000 * kbps; |
| 519 | targetFps = fps; |
| 520 | bitrateAccumulatorMax = targetBitrateBps / 8.0; |
| 521 | bitrateAccumulator = 0; |
| 522 | bitrateObservationTimeMs = 0; |
| 523 | bitrateAdjustmentScaleExp = 0; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 524 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 525 | mediaCodecThread = Thread.currentThread(); |
| 526 | try { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 527 | MediaFormat format = MediaFormat.createVideoFormat(mime, width, height); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 528 | format.setInteger(MediaFormat.KEY_BIT_RATE, targetBitrateBps); |
Alex Glaznev | 8a2cd3d | 2015-08-11 11:32:53 -0700 | [diff] [blame] | 529 | format.setInteger("bitrate-mode", VIDEO_ControlRateConstant); |
glaznev@webrtc.org | a40210a | 2014-06-10 23:48:29 +0000 | [diff] [blame] | 530 | format.setInteger(MediaFormat.KEY_COLOR_FORMAT, properties.colorFormat); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 531 | format.setInteger(MediaFormat.KEY_FRAME_RATE, targetFps); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 532 | format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, keyFrameIntervalSec); |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 533 | if (configureH264HighProfile) { |
| 534 | format.setInteger("profile", VIDEO_AVCProfileHigh); |
| 535 | format.setInteger("level", VIDEO_AVCLevel3); |
| 536 | } |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 537 | Logging.d(TAG, " Format: " + format); |
henrike@webrtc.org | 528fc65 | 2014-10-06 17:56:43 +0000 | [diff] [blame] | 538 | mediaCodec = createByCodecName(properties.codecName); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 539 | this.type = type; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 540 | if (mediaCodec == null) { |
Alex Glaznev | 325d414 | 2015-10-12 14:56:02 -0700 | [diff] [blame] | 541 | Logging.e(TAG, "Can not create media encoder"); |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 542 | release(); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 543 | return false; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 544 | } |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 545 | mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 546 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 547 | if (useSurface) { |
perkj | 48477c1 | 2015-12-18 00:34:37 -0800 | [diff] [blame] | 548 | eglBase = new EglBase14(sharedContext, EglBase.CONFIG_RECORDABLE); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 549 | // Create an input surface and keep a reference since we must release the surface when done. |
| 550 | inputSurface = mediaCodec.createInputSurface(); |
| 551 | eglBase.createSurface(inputSurface); |
| 552 | drawer = new GlRectDrawer(); |
| 553 | } |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 554 | mediaCodec.start(); |
| 555 | outputBuffers = mediaCodec.getOutputBuffers(); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 556 | Logging.d(TAG, "Output buffers: " + outputBuffers.length); |
| 557 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 558 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 559 | Logging.e(TAG, "initEncode failed", e); |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 560 | release(); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 561 | return false; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 562 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 563 | return true; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 566 | @CalledByNativeUnchecked |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 567 | ByteBuffer[] getInputBuffers() { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 568 | ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers(); |
| 569 | Logging.d(TAG, "Input buffers: " + inputBuffers.length); |
| 570 | return inputBuffers; |
| 571 | } |
| 572 | |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 573 | void checkKeyFrameRequired(boolean requestedKeyFrame, long presentationTimestampUs) { |
| 574 | long presentationTimestampMs = (presentationTimestampUs + 500) / 1000; |
| 575 | if (lastKeyFrameMs < 0) { |
| 576 | lastKeyFrameMs = presentationTimestampMs; |
| 577 | } |
| 578 | boolean forcedKeyFrame = false; |
| 579 | if (!requestedKeyFrame && forcedKeyFrameMs > 0 |
| 580 | && presentationTimestampMs > lastKeyFrameMs + forcedKeyFrameMs) { |
| 581 | forcedKeyFrame = true; |
| 582 | } |
| 583 | if (requestedKeyFrame || forcedKeyFrame) { |
| 584 | // Ideally MediaCodec would honor BUFFER_FLAG_SYNC_FRAME so we could |
| 585 | // indicate this in queueInputBuffer() below and guarantee _this_ frame |
| 586 | // be encoded as a key frame, but sadly that flag is ignored. Instead, |
| 587 | // we request a key frame "soon". |
| 588 | if (requestedKeyFrame) { |
| 589 | Logging.d(TAG, "Sync frame request"); |
| 590 | } else { |
| 591 | Logging.d(TAG, "Sync frame forced"); |
| 592 | } |
| 593 | Bundle b = new Bundle(); |
| 594 | b.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0); |
| 595 | mediaCodec.setParameters(b); |
| 596 | lastKeyFrameMs = presentationTimestampMs; |
| 597 | } |
| 598 | } |
| 599 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 600 | @CalledByNativeUnchecked |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 601 | boolean encodeBuffer( |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 602 | boolean isKeyframe, int inputBuffer, int size, long presentationTimestampUs) { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 603 | checkOnMediaCodecThread(); |
| 604 | try { |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 605 | checkKeyFrameRequired(isKeyframe, presentationTimestampUs); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 606 | mediaCodec.queueInputBuffer(inputBuffer, 0, size, presentationTimestampUs, 0); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 607 | return true; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 608 | } catch (IllegalStateException e) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 609 | Logging.e(TAG, "encodeBuffer failed", e); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 610 | return false; |
| 611 | } |
| 612 | } |
| 613 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 614 | @CalledByNativeUnchecked |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 615 | boolean encodeTexture(boolean isKeyframe, int oesTextureId, float[] transformationMatrix, |
| 616 | long presentationTimestampUs) { |
| 617 | checkOnMediaCodecThread(); |
| 618 | try { |
Alex Glaznev | c7483a7 | 2017-01-05 15:22:24 -0800 | [diff] [blame] | 619 | checkKeyFrameRequired(isKeyframe, presentationTimestampUs); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 620 | eglBase.makeCurrent(); |
perkj | 226a602 | 2015-12-02 02:24:40 -0800 | [diff] [blame] | 621 | // TODO(perkj): glClear() shouldn't be necessary since every pixel is covered anyway, |
| 622 | // but it's a workaround for bug webrtc:5147. |
| 623 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
ivoc | 1aa435c | 2016-05-04 07:14:14 -0700 | [diff] [blame] | 624 | drawer.drawOes(oesTextureId, transformationMatrix, width, height, 0, 0, width, height); |
perkj | 48477c1 | 2015-12-18 00:34:37 -0800 | [diff] [blame] | 625 | eglBase.swapBuffers(TimeUnit.MICROSECONDS.toNanos(presentationTimestampUs)); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 626 | return true; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 627 | } catch (RuntimeException e) { |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 628 | Logging.e(TAG, "encodeTexture failed", e); |
| 629 | return false; |
| 630 | } |
| 631 | } |
| 632 | |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 633 | /** |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 634 | * Encodes a new style VideoFrame. |bufferIndex| is -1 if we are not encoding in surface mode. |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 635 | */ |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 636 | @CalledByNativeUnchecked |
Sami Kalliomäki | debbc78 | 2018-02-02 10:01:46 +0100 | [diff] [blame^] | 637 | boolean encodeFrame(long nativeEncoder, boolean isKeyframe, VideoFrame frame, int bufferIndex, |
| 638 | long presentationTimestampUs) { |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 639 | checkOnMediaCodecThread(); |
| 640 | try { |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 641 | checkKeyFrameRequired(isKeyframe, presentationTimestampUs); |
| 642 | |
| 643 | VideoFrame.Buffer buffer = frame.getBuffer(); |
| 644 | if (buffer instanceof VideoFrame.TextureBuffer) { |
| 645 | VideoFrame.TextureBuffer textureBuffer = (VideoFrame.TextureBuffer) buffer; |
| 646 | eglBase.makeCurrent(); |
| 647 | // TODO(perkj): glClear() shouldn't be necessary since every pixel is covered anyway, |
| 648 | // but it's a workaround for bug webrtc:5147. |
| 649 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); |
magjed | 7cede37 | 2017-09-11 06:12:07 -0700 | [diff] [blame] | 650 | VideoFrameDrawer.drawTexture(drawer, textureBuffer, new Matrix() /* renderMatrix */, width, |
sakal | 9bc599f | 2017-09-08 04:46:33 -0700 | [diff] [blame] | 651 | height, 0 /* viewportX */, 0 /* viewportY */, width, height); |
Sami Kalliomäki | debbc78 | 2018-02-02 10:01:46 +0100 | [diff] [blame^] | 652 | eglBase.swapBuffers(TimeUnit.MICROSECONDS.toNanos(presentationTimestampUs)); |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 653 | } else { |
| 654 | VideoFrame.I420Buffer i420Buffer = buffer.toI420(); |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 655 | final int chromaHeight = (height + 1) / 2; |
| 656 | final ByteBuffer dataY = i420Buffer.getDataY(); |
| 657 | final ByteBuffer dataU = i420Buffer.getDataU(); |
| 658 | final ByteBuffer dataV = i420Buffer.getDataV(); |
| 659 | final int strideY = i420Buffer.getStrideY(); |
| 660 | final int strideU = i420Buffer.getStrideU(); |
| 661 | final int strideV = i420Buffer.getStrideV(); |
| 662 | if (dataY.capacity() < strideY * height) { |
| 663 | throw new RuntimeException("Y-plane buffer size too small."); |
| 664 | } |
| 665 | if (dataU.capacity() < strideU * chromaHeight) { |
| 666 | throw new RuntimeException("U-plane buffer size too small."); |
| 667 | } |
| 668 | if (dataV.capacity() < strideV * chromaHeight) { |
| 669 | throw new RuntimeException("V-plane buffer size too small."); |
| 670 | } |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 671 | nativeFillInputBuffer( |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 672 | nativeEncoder, bufferIndex, dataY, strideY, dataU, strideU, dataV, strideV); |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 673 | i420Buffer.release(); |
| 674 | // I420 consists of one full-resolution and two half-resolution planes. |
| 675 | // 1 + 1 / 4 + 1 / 4 = 3 / 2 |
| 676 | int yuvSize = width * height * 3 / 2; |
| 677 | mediaCodec.queueInputBuffer(bufferIndex, 0, yuvSize, presentationTimestampUs, 0); |
| 678 | } |
| 679 | return true; |
| 680 | } catch (RuntimeException e) { |
| 681 | Logging.e(TAG, "encodeFrame failed", e); |
| 682 | return false; |
| 683 | } |
| 684 | } |
| 685 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 686 | @CalledByNativeUnchecked |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 687 | void release() { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 688 | Logging.d(TAG, "Java releaseEncoder"); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 689 | checkOnMediaCodecThread(); |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 690 | |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 691 | class CaughtException { |
| 692 | Exception e; |
| 693 | } |
| 694 | final CaughtException caughtException = new CaughtException(); |
| 695 | boolean stopHung = false; |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 696 | |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 697 | if (mediaCodec != null) { |
| 698 | // Run Mediacodec stop() and release() on separate thread since sometime |
| 699 | // Mediacodec.stop() may hang. |
| 700 | final CountDownLatch releaseDone = new CountDownLatch(1); |
| 701 | |
| 702 | Runnable runMediaCodecRelease = new Runnable() { |
| 703 | @Override |
| 704 | public void run() { |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 705 | Logging.d(TAG, "Java releaseEncoder on release thread"); |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 706 | try { |
| 707 | mediaCodec.stop(); |
| 708 | } catch (Exception e) { |
| 709 | Logging.e(TAG, "Media encoder stop failed", e); |
| 710 | } |
| 711 | try { |
| 712 | mediaCodec.release(); |
| 713 | } catch (Exception e) { |
| 714 | Logging.e(TAG, "Media encoder release failed", e); |
| 715 | caughtException.e = e; |
| 716 | } |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 717 | Logging.d(TAG, "Java releaseEncoder on release thread done"); |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 718 | |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 719 | releaseDone.countDown(); |
| 720 | } |
| 721 | }; |
| 722 | new Thread(runMediaCodecRelease).start(); |
| 723 | |
| 724 | if (!ThreadUtils.awaitUninterruptibly(releaseDone, MEDIA_CODEC_RELEASE_TIMEOUT_MS)) { |
| 725 | Logging.e(TAG, "Media encoder release timeout"); |
| 726 | stopHung = true; |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 727 | } |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 728 | |
| 729 | mediaCodec = null; |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 730 | } |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 731 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 732 | mediaCodecThread = null; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 733 | if (drawer != null) { |
| 734 | drawer.release(); |
| 735 | drawer = null; |
| 736 | } |
| 737 | if (eglBase != null) { |
| 738 | eglBase.release(); |
| 739 | eglBase = null; |
| 740 | } |
| 741 | if (inputSurface != null) { |
| 742 | inputSurface.release(); |
| 743 | inputSurface = null; |
| 744 | } |
Alex Glaznev | c6aec4b | 2015-10-19 16:39:19 -0700 | [diff] [blame] | 745 | runningInstance = null; |
sakal | 996a83c | 2017-03-15 05:53:14 -0700 | [diff] [blame] | 746 | |
| 747 | if (stopHung) { |
| 748 | codecErrors++; |
| 749 | if (errorCallback != null) { |
| 750 | Logging.e(TAG, "Invoke codec error callback. Errors: " + codecErrors); |
| 751 | errorCallback.onMediaCodecVideoEncoderCriticalError(codecErrors); |
| 752 | } |
| 753 | throw new RuntimeException("Media encoder release timeout."); |
| 754 | } |
| 755 | |
| 756 | // Re-throw any runtime exception caught inside the other thread. Since this is an invoke, add |
| 757 | // stack trace for the waiting thread as well. |
| 758 | if (caughtException.e != null) { |
| 759 | final RuntimeException runtimeException = new RuntimeException(caughtException.e); |
| 760 | runtimeException.setStackTrace(ThreadUtils.concatStackTraces( |
| 761 | caughtException.e.getStackTrace(), runtimeException.getStackTrace())); |
| 762 | throw runtimeException; |
| 763 | } |
| 764 | |
Alex Glaznev | 325d414 | 2015-10-12 14:56:02 -0700 | [diff] [blame] | 765 | Logging.d(TAG, "Java releaseEncoder done"); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 768 | @CalledByNativeUnchecked |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 769 | private boolean setRates(int kbps, int frameRate) { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 770 | checkOnMediaCodecThread(); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 771 | |
| 772 | int codecBitrateBps = 1000 * kbps; |
| 773 | if (bitrateAdjustmentType == BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) { |
| 774 | bitrateAccumulatorMax = codecBitrateBps / 8.0; |
| 775 | if (targetBitrateBps > 0 && codecBitrateBps < targetBitrateBps) { |
| 776 | // Rescale the accumulator level if the accumulator max decreases |
| 777 | bitrateAccumulator = bitrateAccumulator * codecBitrateBps / targetBitrateBps; |
| 778 | } |
Alex Glaznev | 269fe75 | 2016-05-25 16:17:33 -0700 | [diff] [blame] | 779 | } |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 780 | targetBitrateBps = codecBitrateBps; |
| 781 | targetFps = frameRate; |
| 782 | |
| 783 | // Adjust actual encoder bitrate based on bitrate adjustment type. |
| 784 | if (bitrateAdjustmentType == BitrateAdjustmentType.FRAMERATE_ADJUSTMENT && targetFps > 0) { |
| 785 | codecBitrateBps = BITRATE_ADJUSTMENT_FPS * targetBitrateBps / targetFps; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 786 | Logging.v(TAG, |
| 787 | "setRates: " + kbps + " -> " + (codecBitrateBps / 1000) + " kbps. Fps: " + targetFps); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 788 | } else if (bitrateAdjustmentType == BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 789 | Logging.v(TAG, "setRates: " + kbps + " kbps. Fps: " + targetFps + ". ExpScale: " |
| 790 | + bitrateAdjustmentScaleExp); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 791 | if (bitrateAdjustmentScaleExp != 0) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 792 | codecBitrateBps = (int) (codecBitrateBps * getBitrateScale(bitrateAdjustmentScaleExp)); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 793 | } |
| 794 | } else { |
| 795 | Logging.v(TAG, "setRates: " + kbps + " kbps. Fps: " + targetFps); |
| 796 | } |
| 797 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 798 | try { |
| 799 | Bundle params = new Bundle(); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 800 | params.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, codecBitrateBps); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 801 | mediaCodec.setParameters(params); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 802 | return true; |
| 803 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 804 | Logging.e(TAG, "setRates failed", e); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 805 | return false; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | // Dequeue an input buffer and return its index, -1 if no input buffer is |
| 810 | // available, or -2 if the codec is no longer operative. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 811 | @CalledByNativeUnchecked |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 812 | int dequeueInputBuffer() { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 813 | checkOnMediaCodecThread(); |
| 814 | try { |
| 815 | return mediaCodec.dequeueInputBuffer(DEQUEUE_TIMEOUT); |
| 816 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 817 | Logging.e(TAG, "dequeueIntputBuffer failed", e); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 818 | return -2; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // Helper struct for dequeueOutputBuffer() below. |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 823 | static class OutputBufferInfo { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 824 | public OutputBufferInfo( |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 825 | int index, ByteBuffer buffer, boolean isKeyFrame, long presentationTimestampUs) { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 826 | this.index = index; |
| 827 | this.buffer = buffer; |
| 828 | this.isKeyFrame = isKeyFrame; |
| 829 | this.presentationTimestampUs = presentationTimestampUs; |
| 830 | } |
| 831 | |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 832 | public final int index; |
| 833 | public final ByteBuffer buffer; |
| 834 | public final boolean isKeyFrame; |
| 835 | public final long presentationTimestampUs; |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 836 | |
| 837 | @CalledByNative("OutputBufferInfo") |
| 838 | int getIndex() { |
| 839 | return index; |
| 840 | } |
| 841 | |
| 842 | @CalledByNative("OutputBufferInfo") |
| 843 | ByteBuffer getBuffer() { |
| 844 | return buffer; |
| 845 | } |
| 846 | |
| 847 | @CalledByNative("OutputBufferInfo") |
| 848 | boolean isKeyFrame() { |
| 849 | return isKeyFrame; |
| 850 | } |
| 851 | |
| 852 | @CalledByNative("OutputBufferInfo") |
| 853 | long getPresentationTimestampUs() { |
| 854 | return presentationTimestampUs; |
| 855 | } |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | // Dequeue and return an output buffer, or null if no output is ready. Return |
| 859 | // a fake OutputBufferInfo with index -1 if the codec is no longer operable. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 860 | @CalledByNativeUnchecked |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 861 | OutputBufferInfo dequeueOutputBuffer() { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 862 | checkOnMediaCodecThread(); |
| 863 | try { |
| 864 | MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); |
| 865 | int result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 866 | // Check if this is config frame and save configuration data. |
| 867 | if (result >= 0) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 868 | boolean isConfigFrame = (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 869 | if (isConfigFrame) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 870 | Logging.d(TAG, "Config frame generated. Offset: " + info.offset + ". Size: " + info.size); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 871 | configData = ByteBuffer.allocateDirect(info.size); |
| 872 | outputBuffers[result].position(info.offset); |
| 873 | outputBuffers[result].limit(info.offset + info.size); |
| 874 | configData.put(outputBuffers[result]); |
glaznev | 3fc2350 | 2017-06-15 16:24:37 -0700 | [diff] [blame] | 875 | // Log few SPS header bytes to check profile and level. |
| 876 | String spsData = ""; |
| 877 | for (int i = 0; i < (info.size < 8 ? info.size : 8); i++) { |
| 878 | spsData += Integer.toHexString(configData.get(i) & 0xff) + " "; |
| 879 | } |
| 880 | Logging.d(TAG, spsData); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 881 | // Release buffer back. |
| 882 | mediaCodec.releaseOutputBuffer(result, false); |
| 883 | // Query next output. |
| 884 | result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT); |
| 885 | } |
| 886 | } |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 887 | if (result >= 0) { |
| 888 | // MediaCodec doesn't care about Buffer position/remaining/etc so we can |
| 889 | // mess with them to get a slice and avoid having to pass extra |
| 890 | // (BufferInfo-related) parameters back to C++. |
| 891 | ByteBuffer outputBuffer = outputBuffers[result].duplicate(); |
| 892 | outputBuffer.position(info.offset); |
| 893 | outputBuffer.limit(info.offset + info.size); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 894 | reportEncodedFrame(info.size); |
| 895 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 896 | // Check key frame flag. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 897 | boolean isKeyFrame = (info.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0; |
glaznev@webrtc.org | c6c1dfd | 2014-06-13 22:59:08 +0000 | [diff] [blame] | 898 | if (isKeyFrame) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 899 | Logging.d(TAG, "Sync frame generated"); |
glaznev@webrtc.org | c6c1dfd | 2014-06-13 22:59:08 +0000 | [diff] [blame] | 900 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 901 | if (isKeyFrame && type == VideoCodecType.VIDEO_CODEC_H264) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 902 | Logging.d(TAG, "Appending config frame of size " + configData.capacity() |
| 903 | + " to output buffer with offset " + info.offset + ", size " + info.size); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 904 | // For H.264 key frame append SPS and PPS NALs at the start |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 905 | ByteBuffer keyFrameBuffer = ByteBuffer.allocateDirect(configData.capacity() + info.size); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 906 | configData.rewind(); |
| 907 | keyFrameBuffer.put(configData); |
| 908 | keyFrameBuffer.put(outputBuffer); |
| 909 | keyFrameBuffer.position(0); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 910 | return new OutputBufferInfo(result, keyFrameBuffer, isKeyFrame, info.presentationTimeUs); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 911 | } else { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 912 | return new OutputBufferInfo( |
| 913 | result, outputBuffer.slice(), isKeyFrame, info.presentationTimeUs); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 914 | } |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 915 | } else if (result == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) { |
| 916 | outputBuffers = mediaCodec.getOutputBuffers(); |
| 917 | return dequeueOutputBuffer(); |
| 918 | } else if (result == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { |
| 919 | return dequeueOutputBuffer(); |
| 920 | } else if (result == MediaCodec.INFO_TRY_AGAIN_LATER) { |
| 921 | return null; |
| 922 | } |
| 923 | throw new RuntimeException("dequeueOutputBuffer: " + result); |
| 924 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 925 | Logging.e(TAG, "dequeueOutputBuffer failed", e); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 926 | return new OutputBufferInfo(-1, null, false, -1); |
| 927 | } |
| 928 | } |
| 929 | |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 930 | private double getBitrateScale(int bitrateAdjustmentScaleExp) { |
| 931 | return Math.pow(BITRATE_CORRECTION_MAX_SCALE, |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 932 | (double) bitrateAdjustmentScaleExp / BITRATE_CORRECTION_STEPS); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | private void reportEncodedFrame(int size) { |
| 936 | if (targetFps == 0 || bitrateAdjustmentType != BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) { |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | // Accumulate the difference between actial and expected frame sizes. |
| 941 | double expectedBytesPerFrame = targetBitrateBps / (8.0 * targetFps); |
| 942 | bitrateAccumulator += (size - expectedBytesPerFrame); |
| 943 | bitrateObservationTimeMs += 1000.0 / targetFps; |
| 944 | |
| 945 | // Put a cap on the accumulator, i.e., don't let it grow beyond some level to avoid |
| 946 | // using too old data for bitrate adjustment. |
| 947 | double bitrateAccumulatorCap = BITRATE_CORRECTION_SEC * bitrateAccumulatorMax; |
| 948 | bitrateAccumulator = Math.min(bitrateAccumulator, bitrateAccumulatorCap); |
| 949 | bitrateAccumulator = Math.max(bitrateAccumulator, -bitrateAccumulatorCap); |
| 950 | |
| 951 | // Do bitrate adjustment every 3 seconds if actual encoder bitrate deviates too much |
| 952 | // form the target value. |
| 953 | if (bitrateObservationTimeMs > 1000 * BITRATE_CORRECTION_SEC) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 954 | Logging.d(TAG, "Acc: " + (int) bitrateAccumulator + ". Max: " + (int) bitrateAccumulatorMax |
| 955 | + ". ExpScale: " + bitrateAdjustmentScaleExp); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 956 | boolean bitrateAdjustmentScaleChanged = false; |
| 957 | if (bitrateAccumulator > bitrateAccumulatorMax) { |
| 958 | // Encoder generates too high bitrate - need to reduce the scale. |
Alex Glaznev | 7fa4a72 | 2017-01-17 15:32:02 -0800 | [diff] [blame] | 959 | int bitrateAdjustmentInc = (int) (bitrateAccumulator / bitrateAccumulatorMax + 0.5); |
| 960 | bitrateAdjustmentScaleExp -= bitrateAdjustmentInc; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 961 | bitrateAccumulator = bitrateAccumulatorMax; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 962 | bitrateAdjustmentScaleChanged = true; |
| 963 | } else if (bitrateAccumulator < -bitrateAccumulatorMax) { |
| 964 | // Encoder generates too low bitrate - need to increase the scale. |
Alex Glaznev | 7fa4a72 | 2017-01-17 15:32:02 -0800 | [diff] [blame] | 965 | int bitrateAdjustmentInc = (int) (-bitrateAccumulator / bitrateAccumulatorMax + 0.5); |
| 966 | bitrateAdjustmentScaleExp += bitrateAdjustmentInc; |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 967 | bitrateAccumulator = -bitrateAccumulatorMax; |
| 968 | bitrateAdjustmentScaleChanged = true; |
| 969 | } |
| 970 | if (bitrateAdjustmentScaleChanged) { |
| 971 | bitrateAdjustmentScaleExp = Math.min(bitrateAdjustmentScaleExp, BITRATE_CORRECTION_STEPS); |
| 972 | bitrateAdjustmentScaleExp = Math.max(bitrateAdjustmentScaleExp, -BITRATE_CORRECTION_STEPS); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 973 | Logging.d(TAG, "Adjusting bitrate scale to " + bitrateAdjustmentScaleExp + ". Value: " |
| 974 | + getBitrateScale(bitrateAdjustmentScaleExp)); |
Alex Glaznev | cfaca03 | 2016-09-06 14:08:19 -0700 | [diff] [blame] | 975 | setRates(targetBitrateBps / 1000, targetFps); |
| 976 | } |
| 977 | bitrateObservationTimeMs = 0; |
| 978 | } |
| 979 | } |
| 980 | |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 981 | // Release a dequeued output buffer back to the codec for re-use. Return |
| 982 | // false if the codec is no longer operable. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 983 | @CalledByNativeUnchecked |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 984 | boolean releaseOutputBuffer(int index) { |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 985 | checkOnMediaCodecThread(); |
| 986 | try { |
| 987 | mediaCodec.releaseOutputBuffer(index, false); |
| 988 | return true; |
| 989 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 990 | Logging.e(TAG, "releaseOutputBuffer failed", e); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 991 | return false; |
| 992 | } |
| 993 | } |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 994 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 995 | @CalledByNative |
| 996 | int getColorFormat() { |
| 997 | return colorFormat; |
| 998 | } |
| 999 | |
| 1000 | @CalledByNative |
| 1001 | static boolean isTextureBuffer(VideoFrame.Buffer buffer) { |
| 1002 | return buffer instanceof VideoFrame.TextureBuffer; |
| 1003 | } |
| 1004 | |
sakal | b5f5bdc | 2017-08-10 04:15:42 -0700 | [diff] [blame] | 1005 | /** Fills an inputBuffer with the given index with data from the byte buffers. */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 1006 | private static native void nativeFillInputBuffer(long encoder, int inputBuffer, ByteBuffer dataY, |
| 1007 | int strideY, ByteBuffer dataU, int strideU, ByteBuffer dataV, int strideV); |
fischman@webrtc.org | 540acde | 2014-02-13 03:56:14 +0000 | [diff] [blame] | 1008 | } |