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