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