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