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