glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2014 The WebRTC project authors. All Rights Reserved. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +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. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 13 | import android.graphics.SurfaceTexture; |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 14 | import android.media.MediaCodec; |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 15 | import android.media.MediaCodecInfo; |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 16 | import android.media.MediaCodecInfo.CodecCapabilities; |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 17 | import android.media.MediaCodecList; |
| 18 | import android.media.MediaFormat; |
| 19 | import android.os.Build; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 20 | import android.os.SystemClock; |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 21 | import android.view.Surface; |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 22 | import java.nio.ByteBuffer; |
Magnus Jedvert | 6062f37 | 2017-11-16 16:53:12 +0100 | [diff] [blame] | 23 | import java.util.ArrayDeque; |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 24 | import java.util.ArrayList; |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 25 | import java.util.Arrays; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 26 | import java.util.HashSet; |
Magnus Jedvert | 91b348c | 2015-10-07 22:57:06 +0200 | [diff] [blame] | 27 | import java.util.List; |
Sami Kalliomaki | d3235f0 | 2016-08-02 15:44:04 +0200 | [diff] [blame] | 28 | import java.util.Queue; |
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; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [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 | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 33 | import org.webrtc.EglBase; |
| 34 | import org.webrtc.VideoFrame; |
magjed | 8c425aa | 2015-10-22 16:52:39 -0700 | [diff] [blame] | 35 | |
Magnus Jedvert | 9060eb1 | 2017-12-12 12:52:54 +0100 | [diff] [blame] | 36 | // Java-side of peerconnection.cc:MediaCodecVideoDecoder. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +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 | @SuppressWarnings("deprecation") |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 39 | @Deprecated |
Alex Glaznev | 908e77b | 2015-04-22 09:25:34 -0700 | [diff] [blame] | 40 | public class MediaCodecVideoDecoder { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 41 | // This class is constructed, operated, and destroyed by its C++ incarnation, |
| 42 | // so the class and its methods have non-public visibility. The API this |
| 43 | // class exposes aims to mimic the webrtc::VideoDecoder API as closely as |
| 44 | // possibly to minimize the amount of translation work necessary. |
| 45 | |
| 46 | private static final String TAG = "MediaCodecVideoDecoder"; |
magjed | cedddbd | 2016-02-26 09:36:04 -0800 | [diff] [blame] | 47 | private static final long MAX_DECODE_TIME_MS = 200; |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 48 | |
magjed | 0c29de5 | 2017-03-02 00:55:32 -0800 | [diff] [blame] | 49 | // TODO(magjed): Use MediaFormat constants when part of the public API. |
| 50 | private static final String FORMAT_KEY_STRIDE = "stride"; |
| 51 | private static final String FORMAT_KEY_SLICE_HEIGHT = "slice-height"; |
| 52 | private static final String FORMAT_KEY_CROP_LEFT = "crop-left"; |
| 53 | private static final String FORMAT_KEY_CROP_RIGHT = "crop-right"; |
| 54 | private static final String FORMAT_KEY_CROP_TOP = "crop-top"; |
| 55 | private static final String FORMAT_KEY_CROP_BOTTOM = "crop-bottom"; |
| 56 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 57 | // Tracks webrtc::VideoCodecType. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 58 | public enum VideoCodecType { |
Niels Möller | 520ca4e | 2018-06-04 11:14:38 +0200 | [diff] [blame] | 59 | VIDEO_CODEC_UNKNOWN, |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 60 | VIDEO_CODEC_VP8, |
| 61 | VIDEO_CODEC_VP9, |
| 62 | VIDEO_CODEC_H264; |
| 63 | |
| 64 | @CalledByNative("VideoCodecType") |
| 65 | static VideoCodecType fromNativeIndex(int nativeIndex) { |
| 66 | return values()[nativeIndex]; |
| 67 | } |
| 68 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 69 | |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 70 | // Timeout for input buffer dequeue. |
| 71 | private static final int DEQUEUE_INPUT_TIMEOUT = 500000; |
| 72 | // Timeout for codec releasing. |
| 73 | private static final int MEDIA_CODEC_RELEASE_TIMEOUT_MS = 5000; |
| 74 | // Max number of output buffers queued before starting to drop decoded frames. |
| 75 | private static final int MAX_QUEUED_OUTPUTBUFFERS = 3; |
Alex Glaznev | c6aec4b | 2015-10-19 16:39:19 -0700 | [diff] [blame] | 76 | // Active running decoder instance. Set in initDecode() (called from native code) |
| 77 | // and reset to null in release() call. |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 78 | @Nullable private static MediaCodecVideoDecoder runningInstance = null; |
| 79 | @Nullable private static MediaCodecVideoDecoderErrorCallback errorCallback = null; |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 80 | private static int codecErrors = 0; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 81 | // List of disabled codec types - can be set from application. |
| 82 | private static Set<String> hwDecoderDisabledTypes = new HashSet<String>(); |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 83 | @Nullable private static EglBase eglBase; |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 84 | |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 85 | @Nullable private Thread mediaCodecThread; |
| 86 | @Nullable private MediaCodec mediaCodec; |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 87 | private ByteBuffer[] inputBuffers; |
| 88 | private ByteBuffer[] outputBuffers; |
| 89 | private static final String VP8_MIME_TYPE = "video/x-vnd.on2.vp8"; |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 90 | 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] | 91 | private static final String H264_MIME_TYPE = "video/avc"; |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 92 | // List of supported HW VP8 decoders. |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 93 | private static final String[] supportedVp8HwCodecPrefixes() { |
| 94 | ArrayList<String> supportedPrefixes = new ArrayList<String>(); |
| 95 | supportedPrefixes.add("OMX.qcom."); |
| 96 | supportedPrefixes.add("OMX.Nvidia."); |
| 97 | supportedPrefixes.add("OMX.Exynos."); |
| 98 | supportedPrefixes.add("OMX.Intel."); |
| 99 | if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-MediaTekVP8").equals("Enabled") |
| 100 | && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 101 | supportedPrefixes.add("OMX.MTK."); |
| 102 | } |
| 103 | return supportedPrefixes.toArray(new String[supportedPrefixes.size()]); |
| 104 | } |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 105 | // List of supported HW VP9 decoders. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 106 | private static final String[] supportedVp9HwCodecPrefixes = {"OMX.qcom.", "OMX.Exynos."}; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 107 | // List of supported HW H.264 decoders. |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 108 | private static final String[] supportedH264HwCodecPrefixes() { |
| 109 | ArrayList<String> supportedPrefixes = new ArrayList<String>(); |
| 110 | supportedPrefixes.add("OMX.qcom."); |
| 111 | supportedPrefixes.add("OMX.Intel."); |
| 112 | supportedPrefixes.add("OMX.Exynos."); |
| 113 | if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-MediaTekH264").equals("Enabled") |
Alex Leung | 28e7107 | 2018-02-05 13:42:48 -0800 | [diff] [blame] | 114 | && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 115 | supportedPrefixes.add("OMX.MTK."); |
| 116 | } |
| 117 | return supportedPrefixes.toArray(new String[supportedPrefixes.size()]); |
| 118 | } |
| 119 | |
glaznev | 0c1d060 | 2017-01-27 12:24:24 -0800 | [diff] [blame] | 120 | // List of supported HW H.264 high profile decoders. |
glaznev | cca0f6c | 2017-06-14 10:20:54 -0700 | [diff] [blame] | 121 | private static final String supportedQcomH264HighProfileHwCodecPrefix = "OMX.qcom."; |
| 122 | private static final String supportedExynosH264HighProfileHwCodecPrefix = "OMX.Exynos."; |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 123 | private static final String supportedMediaTekH264HighProfileHwCodecPrefix = "OMX.MTK."; |
glaznev | 893a7ee | 2016-09-22 10:44:30 -0700 | [diff] [blame] | 124 | |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 125 | // NV12 color format supported by QCOM codec, but not declared in MediaCodec - |
| 126 | // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h |
glaznev | 893a7ee | 2016-09-22 10:44:30 -0700 | [diff] [blame] | 127 | private static final int COLOR_QCOM_FORMATYVU420PackedSemiPlanar32m4ka = 0x7FA30C01; |
| 128 | private static final int COLOR_QCOM_FORMATYVU420PackedSemiPlanar16m4ka = 0x7FA30C02; |
| 129 | private static final int COLOR_QCOM_FORMATYVU420PackedSemiPlanar64x32Tile2m8ka = 0x7FA30C03; |
| 130 | private static final int COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04; |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 131 | // Allowable color formats supported by codec - in order of preference. |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 132 | private static final List<Integer> supportedColorList = Arrays.asList( |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 133 | CodecCapabilities.COLOR_FormatYUV420Planar, CodecCapabilities.COLOR_FormatYUV420SemiPlanar, |
| 134 | CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar, |
| 135 | COLOR_QCOM_FORMATYVU420PackedSemiPlanar32m4ka, COLOR_QCOM_FORMATYVU420PackedSemiPlanar16m4ka, |
| 136 | COLOR_QCOM_FORMATYVU420PackedSemiPlanar64x32Tile2m8ka, |
| 137 | COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m); |
glaznev | 893a7ee | 2016-09-22 10:44:30 -0700 | [diff] [blame] | 138 | |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 139 | private int colorFormat; |
| 140 | private int width; |
| 141 | private int height; |
| 142 | private int stride; |
| 143 | private int sliceHeight; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 144 | private boolean hasDecodedFirstFrame; |
Magnus Jedvert | 6062f37 | 2017-11-16 16:53:12 +0100 | [diff] [blame] | 145 | private final Queue<TimeStamps> decodeStartTimeMs = new ArrayDeque<TimeStamps>(); |
Per | c01c254 | 2015-11-13 16:58:26 +0100 | [diff] [blame] | 146 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 147 | // The below variables are only used when decoding to a Surface. |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 148 | @Nullable private TextureListener textureListener; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 149 | private int droppedFrames; |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 150 | @Nullable private Surface surface = null; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 151 | private final Queue<DecodedOutputBuffer> dequeuedSurfaceOutputBuffers = |
Magnus Jedvert | 6062f37 | 2017-11-16 16:53:12 +0100 | [diff] [blame] | 152 | new ArrayDeque<DecodedOutputBuffer>(); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 153 | |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 154 | // MediaCodec error handler - invoked when critical error happens which may prevent |
| 155 | // further use of media codec API. Now it means that one of media codec instances |
| 156 | // is hanging and can no longer be used in the next call. |
| 157 | public static interface MediaCodecVideoDecoderErrorCallback { |
| 158 | void onMediaCodecVideoDecoderCriticalError(int codecErrors); |
| 159 | } |
| 160 | |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 161 | /** Set EGL context used by HW decoding. The EGL context must be shared with the remote render. */ |
| 162 | public static void setEglContext(EglBase.Context eglContext) { |
| 163 | if (eglBase != null) { |
| 164 | Logging.w(TAG, "Egl context already set."); |
| 165 | eglBase.release(); |
| 166 | } |
| 167 | eglBase = EglBase.create(eglContext); |
| 168 | } |
| 169 | |
| 170 | /** Dispose the EGL context used by HW decoding. */ |
| 171 | public static void disposeEglContext() { |
| 172 | if (eglBase != null) { |
| 173 | eglBase.release(); |
| 174 | eglBase = null; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @CalledByNative |
| 179 | static boolean useSurface() { |
| 180 | return eglBase != null; |
| 181 | } |
| 182 | |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 183 | public static void setErrorCallback(MediaCodecVideoDecoderErrorCallback errorCallback) { |
| 184 | Logging.d(TAG, "Set error callback"); |
| 185 | MediaCodecVideoDecoder.errorCallback = errorCallback; |
| 186 | } |
| 187 | |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 188 | // Functions to disable HW decoding - can be called from applications for platforms |
| 189 | // which have known HW decoding problems. |
| 190 | public static void disableVp8HwCodec() { |
| 191 | Logging.w(TAG, "VP8 decoding is disabled by application."); |
| 192 | hwDecoderDisabledTypes.add(VP8_MIME_TYPE); |
| 193 | } |
| 194 | |
| 195 | public static void disableVp9HwCodec() { |
| 196 | Logging.w(TAG, "VP9 decoding is disabled by application."); |
| 197 | hwDecoderDisabledTypes.add(VP9_MIME_TYPE); |
| 198 | } |
| 199 | |
| 200 | public static void disableH264HwCodec() { |
| 201 | Logging.w(TAG, "H.264 decoding is disabled by application."); |
| 202 | hwDecoderDisabledTypes.add(H264_MIME_TYPE); |
| 203 | } |
| 204 | |
| 205 | // Functions to query if HW decoding is supported. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 206 | @CalledByNativeUnchecked |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 207 | public static boolean isVp8HwSupported() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 208 | return !hwDecoderDisabledTypes.contains(VP8_MIME_TYPE) |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 209 | && (findDecoder(VP8_MIME_TYPE, supportedVp8HwCodecPrefixes()) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 212 | @CalledByNativeUnchecked |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 213 | public static boolean isVp9HwSupported() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 214 | return !hwDecoderDisabledTypes.contains(VP9_MIME_TYPE) |
| 215 | && (findDecoder(VP9_MIME_TYPE, supportedVp9HwCodecPrefixes) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 218 | @CalledByNativeUnchecked |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 219 | public static boolean isH264HwSupported() { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 220 | return !hwDecoderDisabledTypes.contains(H264_MIME_TYPE) |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 221 | && (findDecoder(H264_MIME_TYPE, supportedH264HwCodecPrefixes()) != null); |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 224 | @CalledByNative |
glaznev | 0c1d060 | 2017-01-27 12:24:24 -0800 | [diff] [blame] | 225 | public static boolean isH264HighProfileHwSupported() { |
glaznev | cca0f6c | 2017-06-14 10:20:54 -0700 | [diff] [blame] | 226 | if (hwDecoderDisabledTypes.contains(H264_MIME_TYPE)) { |
| 227 | return false; |
| 228 | } |
| 229 | // Support H.264 HP decoding on QCOM chips for Android L and above. |
| 230 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP |
| 231 | && findDecoder(H264_MIME_TYPE, new String[] {supportedQcomH264HighProfileHwCodecPrefix}) |
| 232 | != null) { |
| 233 | return true; |
| 234 | } |
| 235 | // Support H.264 HP decoding on Exynos chips for Android M and above. |
| 236 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M |
| 237 | && findDecoder(H264_MIME_TYPE, new String[] {supportedExynosH264HighProfileHwCodecPrefix}) |
| 238 | != null) { |
| 239 | return true; |
| 240 | } |
Alex Leung | 28e7107 | 2018-02-05 13:42:48 -0800 | [diff] [blame] | 241 | // Support H.264 HP decoding on MediaTek chips for Android O_MR1 and above |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 242 | if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-MediaTekH264").equals("Enabled") |
Alex Leung | 28e7107 | 2018-02-05 13:42:48 -0800 | [diff] [blame] | 243 | && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 244 | && findDecoder(H264_MIME_TYPE, new String[] {supportedMediaTekH264HighProfileHwCodecPrefix}) |
| 245 | != null) { |
| 246 | return true; |
| 247 | } |
glaznev | cca0f6c | 2017-06-14 10:20:54 -0700 | [diff] [blame] | 248 | return false; |
glaznev | 0c1d060 | 2017-01-27 12:24:24 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 251 | public static void printStackTrace() { |
| 252 | if (runningInstance != null && runningInstance.mediaCodecThread != null) { |
| 253 | StackTraceElement[] mediaCodecStackTraces = runningInstance.mediaCodecThread.getStackTrace(); |
| 254 | if (mediaCodecStackTraces.length > 0) { |
| 255 | Logging.d(TAG, "MediaCodecVideoDecoder stacks trace:"); |
| 256 | for (StackTraceElement stackTrace : mediaCodecStackTraces) { |
| 257 | Logging.d(TAG, stackTrace.toString()); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Helper struct for findDecoder() below. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 264 | private static class DecoderProperties { |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 265 | public DecoderProperties(String codecName, int colorFormat) { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 266 | this.codecName = codecName; |
| 267 | this.colorFormat = colorFormat; |
| 268 | } |
| 269 | public final String codecName; // OpenMax component name for VP8 codec. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 270 | public final int colorFormat; // Color format supported by codec. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 273 | private static @Nullable DecoderProperties findDecoder( |
| 274 | String mime, String[] supportedCodecPrefixes) { |
glaznev@webrtc.org | 25cc745 | 2014-10-02 16:58:05 +0000 | [diff] [blame] | 275 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 276 | return null; // MediaCodec.setParameters is missing. |
glaznev@webrtc.org | 25cc745 | 2014-10-02 16:58:05 +0000 | [diff] [blame] | 277 | } |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 278 | Logging.d(TAG, "Trying to find HW decoder for mime " + mime); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 279 | for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) { |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 280 | MediaCodecInfo info = null; |
| 281 | try { |
| 282 | info = MediaCodecList.getCodecInfoAt(i); |
| 283 | } catch (IllegalArgumentException e) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 284 | Logging.e(TAG, "Cannot retrieve decoder codec info", e); |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 285 | } |
| 286 | if (info == null || info.isEncoder()) { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | String name = null; |
| 290 | for (String mimeType : info.getSupportedTypes()) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 291 | if (mimeType.equals(mime)) { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 292 | name = info.getName(); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | if (name == null) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 297 | continue; // No HW support in this codec; try the next one. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 298 | } |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 299 | Logging.d(TAG, "Found candidate decoder " + name); |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 300 | |
glaznev@webrtc.org | 25cc745 | 2014-10-02 16:58:05 +0000 | [diff] [blame] | 301 | // Check if this is supported decoder. |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 302 | boolean supportedCodec = false; |
glaznev@webrtc.org | 25cc745 | 2014-10-02 16:58:05 +0000 | [diff] [blame] | 303 | for (String codecPrefix : supportedCodecPrefixes) { |
| 304 | if (name.startsWith(codecPrefix)) { |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 305 | supportedCodec = true; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | if (!supportedCodec) { |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | // Check if codec supports either yuv420 or nv12. |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 314 | CodecCapabilities capabilities; |
| 315 | try { |
| 316 | capabilities = info.getCapabilitiesForType(mime); |
| 317 | } catch (IllegalArgumentException e) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 318 | Logging.e(TAG, "Cannot retrieve decoder capabilities", e); |
Alex Glaznev | 0060c56 | 2016-08-08 12:27:24 -0700 | [diff] [blame] | 319 | continue; |
| 320 | } |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 321 | for (int colorFormat : capabilities.colorFormats) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 322 | Logging.v(TAG, " Color: 0x" + Integer.toHexString(colorFormat)); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 323 | } |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 324 | for (int supportedColorFormat : supportedColorList) { |
| 325 | for (int codecColorFormat : capabilities.colorFormats) { |
| 326 | if (codecColorFormat == supportedColorFormat) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 327 | // Found supported HW decoder. |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 328 | Logging.d(TAG, "Found target decoder " + name + ". Color: 0x" |
| 329 | + Integer.toHexString(codecColorFormat)); |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 330 | return new DecoderProperties(name, codecColorFormat); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 335 | Logging.d(TAG, "No HW decoder found for mime " + mime); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 336 | return null; // No HW decoder. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 339 | @CalledByNative |
| 340 | MediaCodecVideoDecoder() {} |
| 341 | |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 342 | private void checkOnMediaCodecThread() throws IllegalStateException { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 343 | if (mediaCodecThread.getId() != Thread.currentThread().getId()) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 344 | throw new IllegalStateException("MediaCodecVideoDecoder previously operated on " |
| 345 | + mediaCodecThread + " but is now called on " + Thread.currentThread()); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 349 | @CalledByNativeUnchecked |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 350 | private boolean initDecode(VideoCodecType type, int width, int height) { |
Alejandro Luebs | 69ddaef | 2015-10-09 15:46:09 -0700 | [diff] [blame] | 351 | if (mediaCodecThread != null) { |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 352 | throw new RuntimeException("initDecode: Forgot to release()?"); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 353 | } |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 354 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 355 | String mime = null; |
| 356 | String[] supportedCodecPrefixes = null; |
| 357 | if (type == VideoCodecType.VIDEO_CODEC_VP8) { |
| 358 | mime = VP8_MIME_TYPE; |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 359 | supportedCodecPrefixes = supportedVp8HwCodecPrefixes(); |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 360 | } else if (type == VideoCodecType.VIDEO_CODEC_VP9) { |
| 361 | mime = VP9_MIME_TYPE; |
| 362 | supportedCodecPrefixes = supportedVp9HwCodecPrefixes; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 363 | } else if (type == VideoCodecType.VIDEO_CODEC_H264) { |
| 364 | mime = H264_MIME_TYPE; |
Alex Leung | 5b6891a | 2018-01-18 10:01:14 -0800 | [diff] [blame] | 365 | supportedCodecPrefixes = supportedH264HwCodecPrefixes(); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 366 | } else { |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 367 | throw new RuntimeException("initDecode: Non-supported codec " + type); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 368 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 369 | DecoderProperties properties = findDecoder(mime, supportedCodecPrefixes); |
| 370 | if (properties == null) { |
| 371 | throw new RuntimeException("Cannot find HW decoder for " + type); |
| 372 | } |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 373 | |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 374 | Logging.d(TAG, |
| 375 | "Java initDecode: " + type + " : " + width + " x " + height + ". Color: 0x" |
| 376 | + Integer.toHexString(properties.colorFormat) + ". Use Surface: " + useSurface()); |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 377 | |
Alex Glaznev | c6aec4b | 2015-10-19 16:39:19 -0700 | [diff] [blame] | 378 | runningInstance = this; // Decoder is now running and can be queried for stack traces. |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 379 | mediaCodecThread = Thread.currentThread(); |
| 380 | try { |
| 381 | this.width = width; |
| 382 | this.height = height; |
| 383 | stride = width; |
| 384 | sliceHeight = height; |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 385 | |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 386 | if (useSurface()) { |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 387 | @Nullable |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 388 | final SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create( |
| 389 | "Decoder SurfaceTextureHelper", eglBase.getEglBaseContext()); |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 390 | if (surfaceTextureHelper != null) { |
| 391 | textureListener = new TextureListener(surfaceTextureHelper); |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 392 | textureListener.setSize(width, height); |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 393 | surface = new Surface(surfaceTextureHelper.getSurfaceTexture()); |
| 394 | } |
Magnus Jedvert | 80cf97c | 2015-06-11 10:08:59 +0200 | [diff] [blame] | 395 | } |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 396 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 397 | MediaFormat format = MediaFormat.createVideoFormat(mime, width, height); |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 398 | if (!useSurface()) { |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 399 | format.setInteger(MediaFormat.KEY_COLOR_FORMAT, properties.colorFormat); |
| 400 | } |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 401 | Logging.d(TAG, " Format: " + format); |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 402 | mediaCodec = MediaCodecVideoEncoder.createByCodecName(properties.codecName); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 403 | if (mediaCodec == null) { |
Alex Glaznev | 325d414 | 2015-10-12 14:56:02 -0700 | [diff] [blame] | 404 | Logging.e(TAG, "Can not create media decoder"); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 405 | return false; |
| 406 | } |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 407 | mediaCodec.configure(format, surface, null, 0); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 408 | mediaCodec.start(); |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 409 | |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 410 | colorFormat = properties.colorFormat; |
| 411 | outputBuffers = mediaCodec.getOutputBuffers(); |
| 412 | inputBuffers = mediaCodec.getInputBuffers(); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 413 | decodeStartTimeMs.clear(); |
| 414 | hasDecodedFirstFrame = false; |
| 415 | dequeuedSurfaceOutputBuffers.clear(); |
| 416 | droppedFrames = 0; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 417 | Logging.d(TAG, |
| 418 | "Input buffers: " + inputBuffers.length + ". Output buffers: " + outputBuffers.length); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 419 | return true; |
| 420 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 421 | Logging.e(TAG, "initDecode failed", e); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 426 | // Resets the decoder so it can start decoding frames with new resolution. |
| 427 | // Flushes MediaCodec and clears decoder output buffers. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 428 | @CalledByNativeUnchecked |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 429 | private void reset(int width, int height) { |
| 430 | if (mediaCodecThread == null || mediaCodec == null) { |
| 431 | throw new RuntimeException("Incorrect reset call for non-initialized decoder."); |
| 432 | } |
| 433 | Logging.d(TAG, "Java reset: " + width + " x " + height); |
| 434 | |
| 435 | mediaCodec.flush(); |
| 436 | |
| 437 | this.width = width; |
| 438 | this.height = height; |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 439 | if (textureListener != null) { |
| 440 | textureListener.setSize(width, height); |
| 441 | } |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 442 | decodeStartTimeMs.clear(); |
| 443 | dequeuedSurfaceOutputBuffers.clear(); |
| 444 | hasDecodedFirstFrame = false; |
| 445 | droppedFrames = 0; |
| 446 | } |
| 447 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 448 | @CalledByNativeUnchecked |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 449 | private void release() { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 450 | Logging.d(TAG, "Java releaseDecoder. Total number of dropped frames: " + droppedFrames); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 451 | checkOnMediaCodecThread(); |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 452 | |
| 453 | // Run Mediacodec stop() and release() on separate thread since sometime |
| 454 | // Mediacodec.stop() may hang. |
| 455 | final CountDownLatch releaseDone = new CountDownLatch(1); |
| 456 | |
| 457 | Runnable runMediaCodecRelease = new Runnable() { |
| 458 | @Override |
| 459 | public void run() { |
| 460 | try { |
| 461 | Logging.d(TAG, "Java releaseDecoder on release thread"); |
| 462 | mediaCodec.stop(); |
| 463 | mediaCodec.release(); |
| 464 | Logging.d(TAG, "Java releaseDecoder on release thread done"); |
| 465 | } catch (Exception e) { |
| 466 | Logging.e(TAG, "Media decoder release failed", e); |
| 467 | } |
| 468 | releaseDone.countDown(); |
| 469 | } |
| 470 | }; |
| 471 | new Thread(runMediaCodecRelease).start(); |
| 472 | |
| 473 | if (!ThreadUtils.awaitUninterruptibly(releaseDone, MEDIA_CODEC_RELEASE_TIMEOUT_MS)) { |
| 474 | Logging.e(TAG, "Media decoder release timeout"); |
| 475 | codecErrors++; |
| 476 | if (errorCallback != null) { |
| 477 | Logging.e(TAG, "Invoke codec error callback. Errors: " + codecErrors); |
| 478 | errorCallback.onMediaCodecVideoDecoderCriticalError(codecErrors); |
| 479 | } |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 480 | } |
Alex Glaznev | 5c3da4b | 2015-10-30 15:31:07 -0700 | [diff] [blame] | 481 | |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 482 | mediaCodec = null; |
| 483 | mediaCodecThread = null; |
Alex Glaznev | c6aec4b | 2015-10-19 16:39:19 -0700 | [diff] [blame] | 484 | runningInstance = null; |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 485 | if (useSurface()) { |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 486 | surface.release(); |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 487 | surface = null; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 488 | textureListener.release(); |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 489 | } |
Alex Glaznev | 325d414 | 2015-10-12 14:56:02 -0700 | [diff] [blame] | 490 | Logging.d(TAG, "Java releaseDecoder done"); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Dequeue an input buffer and return its index, -1 if no input buffer is |
| 494 | // available, or -2 if the codec is no longer operative. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 495 | @CalledByNativeUnchecked |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 496 | private int dequeueInputBuffer() { |
| 497 | checkOnMediaCodecThread(); |
| 498 | try { |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 499 | return mediaCodec.dequeueInputBuffer(DEQUEUE_INPUT_TIMEOUT); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 500 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 501 | Logging.e(TAG, "dequeueIntputBuffer failed", e); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 502 | return -2; |
| 503 | } |
| 504 | } |
| 505 | |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 506 | @CalledByNativeUnchecked |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 507 | private boolean queueInputBuffer(int inputBufferIndex, int size, long presentationTimeStamUs, |
| 508 | long timeStampMs, long ntpTimeStamp) { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 509 | checkOnMediaCodecThread(); |
| 510 | try { |
| 511 | inputBuffers[inputBufferIndex].position(0); |
| 512 | inputBuffers[inputBufferIndex].limit(size); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 513 | decodeStartTimeMs.add( |
| 514 | new TimeStamps(SystemClock.elapsedRealtime(), timeStampMs, ntpTimeStamp)); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 515 | mediaCodec.queueInputBuffer(inputBufferIndex, 0, size, presentationTimeStamUs, 0); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 516 | return true; |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 517 | } catch (IllegalStateException e) { |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 518 | Logging.e(TAG, "decode failed", e); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | } |
| 522 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 523 | private static class TimeStamps { |
| 524 | public TimeStamps(long decodeStartTimeMs, long timeStampMs, long ntpTimeStampMs) { |
| 525 | this.decodeStartTimeMs = decodeStartTimeMs; |
| 526 | this.timeStampMs = timeStampMs; |
| 527 | this.ntpTimeStampMs = ntpTimeStampMs; |
| 528 | } |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 529 | // Time when this frame was queued for decoding. |
| 530 | private final long decodeStartTimeMs; |
| 531 | // Only used for bookkeeping in Java. Stores C++ inputImage._timeStamp value for input frame. |
| 532 | private final long timeStampMs; |
| 533 | // Only used for bookkeeping in Java. Stores C++ inputImage.ntp_time_ms_ value for input frame. |
| 534 | private final long ntpTimeStampMs; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // Helper struct for dequeueOutputBuffer() below. |
| 538 | private static class DecodedOutputBuffer { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 539 | public DecodedOutputBuffer(int index, int offset, int size, long presentationTimeStampMs, |
| 540 | long timeStampMs, long ntpTimeStampMs, long decodeTime, long endDecodeTime) { |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 541 | this.index = index; |
| 542 | this.offset = offset; |
| 543 | this.size = size; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 544 | this.presentationTimeStampMs = presentationTimeStampMs; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 545 | this.timeStampMs = timeStampMs; |
| 546 | this.ntpTimeStampMs = ntpTimeStampMs; |
| 547 | this.decodeTimeMs = decodeTime; |
| 548 | this.endDecodeTimeMs = endDecodeTime; |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | private final int index; |
| 552 | private final int offset; |
| 553 | private final int size; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 554 | // Presentation timestamp returned in dequeueOutputBuffer call. |
| 555 | private final long presentationTimeStampMs; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 556 | // C++ inputImage._timeStamp value for output frame. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 557 | private final long timeStampMs; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 558 | // C++ inputImage.ntp_time_ms_ value for output frame. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 559 | private final long ntpTimeStampMs; |
| 560 | // Number of ms it took to decode this frame. |
| 561 | private final long decodeTimeMs; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 562 | // System time when this frame decoding finished. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 563 | private final long endDecodeTimeMs; |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 564 | |
| 565 | @CalledByNative("DecodedOutputBuffer") |
| 566 | int getIndex() { |
| 567 | return index; |
| 568 | } |
| 569 | |
| 570 | @CalledByNative("DecodedOutputBuffer") |
| 571 | int getOffset() { |
| 572 | return offset; |
| 573 | } |
| 574 | |
| 575 | @CalledByNative("DecodedOutputBuffer") |
| 576 | int getSize() { |
| 577 | return size; |
| 578 | } |
| 579 | |
| 580 | @CalledByNative("DecodedOutputBuffer") |
| 581 | long getPresentationTimestampMs() { |
| 582 | return presentationTimeStampMs; |
| 583 | } |
| 584 | |
| 585 | @CalledByNative("DecodedOutputBuffer") |
| 586 | long getTimestampMs() { |
| 587 | return timeStampMs; |
| 588 | } |
| 589 | |
| 590 | @CalledByNative("DecodedOutputBuffer") |
| 591 | long getNtpTimestampMs() { |
| 592 | return ntpTimeStampMs; |
| 593 | } |
| 594 | |
| 595 | @CalledByNative("DecodedOutputBuffer") |
| 596 | long getDecodeTimeMs() { |
| 597 | return decodeTimeMs; |
| 598 | } |
glaznev@webrtc.org | 9967845 | 2014-09-15 17:52:42 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 601 | // Helper struct for dequeueTextureBuffer() below. |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 602 | private static class DecodedTextureBuffer { |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 603 | private final VideoFrame.Buffer videoFrameBuffer; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 604 | // Presentation timestamp returned in dequeueOutputBuffer call. |
| 605 | private final long presentationTimeStampMs; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 606 | // C++ inputImage._timeStamp value for output frame. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 607 | private final long timeStampMs; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 608 | // C++ inputImage.ntp_time_ms_ value for output frame. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 609 | private final long ntpTimeStampMs; |
Alex Glaznev | eee86a6 | 2016-01-29 14:17:07 -0800 | [diff] [blame] | 610 | // Number of ms it took to decode this frame. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 611 | private final long decodeTimeMs; |
| 612 | // Interval from when the frame finished decoding until this buffer has been created. |
| 613 | // Since there is only one texture, this interval depend on the time from when |
| 614 | // a frame is decoded and provided to C++ and until that frame is returned to the MediaCodec |
| 615 | // so that the texture can be updated with the next decoded frame. |
| 616 | private final long frameDelayMs; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 617 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 618 | // A DecodedTextureBuffer with zero |textureID| has special meaning and represents a frame |
| 619 | // that was dropped. |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 620 | public DecodedTextureBuffer(VideoFrame.Buffer videoFrameBuffer, long presentationTimeStampMs, |
| 621 | long timeStampMs, long ntpTimeStampMs, long decodeTimeMs, long frameDelay) { |
| 622 | this.videoFrameBuffer = videoFrameBuffer; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 623 | this.presentationTimeStampMs = presentationTimeStampMs; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 624 | this.timeStampMs = timeStampMs; |
| 625 | this.ntpTimeStampMs = ntpTimeStampMs; |
| 626 | this.decodeTimeMs = decodeTimeMs; |
| 627 | this.frameDelayMs = frameDelay; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 628 | } |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 629 | |
| 630 | @CalledByNative("DecodedTextureBuffer") |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 631 | VideoFrame.Buffer getVideoFrameBuffer() { |
| 632 | return videoFrameBuffer; |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | @CalledByNative("DecodedTextureBuffer") |
| 636 | long getPresentationTimestampMs() { |
| 637 | return presentationTimeStampMs; |
| 638 | } |
| 639 | |
| 640 | @CalledByNative("DecodedTextureBuffer") |
| 641 | long getTimeStampMs() { |
| 642 | return timeStampMs; |
| 643 | } |
| 644 | |
| 645 | @CalledByNative("DecodedTextureBuffer") |
| 646 | long getNtpTimestampMs() { |
| 647 | return ntpTimeStampMs; |
| 648 | } |
| 649 | |
| 650 | @CalledByNative("DecodedTextureBuffer") |
| 651 | long getDecodeTimeMs() { |
| 652 | return decodeTimeMs; |
| 653 | } |
| 654 | |
| 655 | @CalledByNative("DecodedTextureBuffer") |
| 656 | long getFrameDelayMs() { |
| 657 | return frameDelayMs; |
| 658 | } |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 661 | // Poll based texture listener. |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 662 | private class TextureListener implements VideoSink { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 663 | private final SurfaceTextureHelper surfaceTextureHelper; |
| 664 | // |newFrameLock| is used to synchronize arrival of new frames with wait()/notifyAll(). |
| 665 | private final Object newFrameLock = new Object(); |
| 666 | // |bufferToRender| is non-null when waiting for transition between addBufferToRender() to |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 667 | // onFrame(). |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 668 | @Nullable private DecodedOutputBuffer bufferToRender; |
| 669 | @Nullable private DecodedTextureBuffer renderedBuffer; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 670 | |
| 671 | public TextureListener(SurfaceTextureHelper surfaceTextureHelper) { |
| 672 | this.surfaceTextureHelper = surfaceTextureHelper; |
magjed | 81e8e37 | 2016-03-03 02:11:44 -0800 | [diff] [blame] | 673 | surfaceTextureHelper.startListening(this); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | public void addBufferToRender(DecodedOutputBuffer buffer) { |
| 677 | if (bufferToRender != null) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 678 | Logging.e(TAG, "Unexpected addBufferToRender() called while waiting for a texture."); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 679 | throw new IllegalStateException("Waiting for a texture."); |
| 680 | } |
| 681 | bufferToRender = buffer; |
| 682 | } |
| 683 | |
| 684 | public boolean isWaitingForTexture() { |
| 685 | synchronized (newFrameLock) { |
| 686 | return bufferToRender != null; |
| 687 | } |
| 688 | } |
| 689 | |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 690 | public void setSize(int width, int height) { |
| 691 | surfaceTextureHelper.setTextureSize(width, height); |
| 692 | } |
| 693 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 694 | // Callback from |surfaceTextureHelper|. May be called on an arbitrary thread. |
| 695 | @Override |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 696 | public void onFrame(VideoFrame frame) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 697 | synchronized (newFrameLock) { |
| 698 | if (renderedBuffer != null) { |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 699 | Logging.e(TAG, "Unexpected onFrame() called while already holding a texture."); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 700 | throw new IllegalStateException("Already holding a texture."); |
| 701 | } |
| 702 | // |timestampNs| is always zero on some Android versions. |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 703 | final VideoFrame.Buffer buffer = frame.getBuffer(); |
| 704 | buffer.retain(); |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 705 | renderedBuffer = new DecodedTextureBuffer(buffer, bufferToRender.presentationTimeStampMs, |
| 706 | bufferToRender.timeStampMs, bufferToRender.ntpTimeStampMs, bufferToRender.decodeTimeMs, |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 707 | SystemClock.elapsedRealtime() - bufferToRender.endDecodeTimeMs); |
| 708 | bufferToRender = null; |
| 709 | newFrameLock.notifyAll(); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // Dequeues and returns a DecodedTextureBuffer if available, or null otherwise. |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 714 | @Nullable |
Sami Kalliomäki | 9828beb | 2017-10-26 16:21:22 +0200 | [diff] [blame] | 715 | @SuppressWarnings("WaitNotInLoop") |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 716 | public DecodedTextureBuffer dequeueTextureBuffer(int timeoutMs) { |
| 717 | synchronized (newFrameLock) { |
| 718 | if (renderedBuffer == null && timeoutMs > 0 && isWaitingForTexture()) { |
| 719 | try { |
| 720 | newFrameLock.wait(timeoutMs); |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 721 | } catch (InterruptedException e) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 722 | // Restore the interrupted status by reinterrupting the thread. |
| 723 | Thread.currentThread().interrupt(); |
| 724 | } |
| 725 | } |
| 726 | DecodedTextureBuffer returnedBuffer = renderedBuffer; |
| 727 | renderedBuffer = null; |
| 728 | return returnedBuffer; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | public void release() { |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 733 | // SurfaceTextureHelper.stopListening() will block until any onFrame() in progress is done. |
| 734 | // Therefore, the call must be outside any synchronized statement that is also used in the |
| 735 | // onFrame() above to avoid deadlocks. |
nisse | a44e72c | 2016-05-27 00:27:59 -0700 | [diff] [blame] | 736 | surfaceTextureHelper.stopListening(); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 737 | synchronized (newFrameLock) { |
| 738 | if (renderedBuffer != null) { |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 739 | renderedBuffer.getVideoFrameBuffer().release(); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 740 | renderedBuffer = null; |
| 741 | } |
| 742 | } |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 743 | surfaceTextureHelper.dispose(); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | // Returns null if no decoded buffer is available, and otherwise a DecodedByteBuffer. |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 748 | // Throws IllegalStateException if call is made on the wrong thread, if color format changes to an |
| 749 | // unsupported format, or if |mediaCodec| is not in the Executing state. Throws CodecException |
| 750 | // upon codec error. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 751 | @CalledByNativeUnchecked |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 752 | private @Nullable DecodedOutputBuffer dequeueOutputBuffer(int dequeueTimeoutMs) { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 753 | checkOnMediaCodecThread(); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 754 | if (decodeStartTimeMs.isEmpty()) { |
| 755 | return null; |
| 756 | } |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 757 | // Drain the decoder until receiving a decoded buffer or hitting |
| 758 | // MediaCodec.INFO_TRY_AGAIN_LATER. |
| 759 | final MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); |
| 760 | while (true) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 761 | final int result = |
| 762 | mediaCodec.dequeueOutputBuffer(info, TimeUnit.MILLISECONDS.toMicros(dequeueTimeoutMs)); |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 763 | switch (result) { |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 764 | case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED: |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 765 | outputBuffers = mediaCodec.getOutputBuffers(); |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 766 | Logging.d(TAG, "Decoder output buffers changed: " + outputBuffers.length); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 767 | if (hasDecodedFirstFrame) { |
| 768 | throw new RuntimeException("Unexpected output buffer change event."); |
| 769 | } |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 770 | break; |
| 771 | case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED: |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 772 | MediaFormat format = mediaCodec.getOutputFormat(); |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 773 | Logging.d(TAG, "Decoder format changed: " + format.toString()); |
magjed | 0c29de5 | 2017-03-02 00:55:32 -0800 | [diff] [blame] | 774 | final int newWidth; |
| 775 | final int newHeight; |
| 776 | if (format.containsKey(FORMAT_KEY_CROP_LEFT) && format.containsKey(FORMAT_KEY_CROP_RIGHT) |
| 777 | && format.containsKey(FORMAT_KEY_CROP_BOTTOM) |
| 778 | && format.containsKey(FORMAT_KEY_CROP_TOP)) { |
| 779 | newWidth = 1 + format.getInteger(FORMAT_KEY_CROP_RIGHT) |
| 780 | - format.getInteger(FORMAT_KEY_CROP_LEFT); |
| 781 | newHeight = 1 + format.getInteger(FORMAT_KEY_CROP_BOTTOM) |
| 782 | - format.getInteger(FORMAT_KEY_CROP_TOP); |
| 783 | } else { |
| 784 | newWidth = format.getInteger(MediaFormat.KEY_WIDTH); |
| 785 | newHeight = format.getInteger(MediaFormat.KEY_HEIGHT); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 786 | } |
magjed | 0c29de5 | 2017-03-02 00:55:32 -0800 | [diff] [blame] | 787 | if (hasDecodedFirstFrame && (newWidth != width || newHeight != height)) { |
| 788 | throw new RuntimeException("Unexpected size change. Configured " + width + "*" + height |
| 789 | + ". New " + newWidth + "*" + newHeight); |
| 790 | } |
| 791 | width = newWidth; |
| 792 | height = newHeight; |
Magnus Jedvert | 80e7a7f | 2018-07-06 11:15:13 +0200 | [diff] [blame] | 793 | if (textureListener != null) { |
| 794 | textureListener.setSize(width, height); |
| 795 | } |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 796 | |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 797 | if (!useSurface() && format.containsKey(MediaFormat.KEY_COLOR_FORMAT)) { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 798 | colorFormat = format.getInteger(MediaFormat.KEY_COLOR_FORMAT); |
Jiayang Liu | 5975b3c | 2015-09-16 13:40:53 -0700 | [diff] [blame] | 799 | Logging.d(TAG, "Color: 0x" + Integer.toHexString(colorFormat)); |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 800 | if (!supportedColorList.contains(colorFormat)) { |
| 801 | throw new IllegalStateException("Non supported color format: " + colorFormat); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 802 | } |
| 803 | } |
magjed | 0c29de5 | 2017-03-02 00:55:32 -0800 | [diff] [blame] | 804 | if (format.containsKey(FORMAT_KEY_STRIDE)) { |
| 805 | stride = format.getInteger(FORMAT_KEY_STRIDE); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 806 | } |
magjed | 0c29de5 | 2017-03-02 00:55:32 -0800 | [diff] [blame] | 807 | if (format.containsKey(FORMAT_KEY_SLICE_HEIGHT)) { |
| 808 | sliceHeight = format.getInteger(FORMAT_KEY_SLICE_HEIGHT); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 809 | } |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 810 | Logging.d(TAG, "Frame stride and slice height: " + stride + " x " + sliceHeight); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 811 | stride = Math.max(width, stride); |
| 812 | sliceHeight = Math.max(height, sliceHeight); |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 813 | break; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 814 | case MediaCodec.INFO_TRY_AGAIN_LATER: |
| 815 | return null; |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 816 | default: |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 817 | hasDecodedFirstFrame = true; |
| 818 | TimeStamps timeStamps = decodeStartTimeMs.remove(); |
magjed | cedddbd | 2016-02-26 09:36:04 -0800 | [diff] [blame] | 819 | long decodeTimeMs = SystemClock.elapsedRealtime() - timeStamps.decodeStartTimeMs; |
| 820 | if (decodeTimeMs > MAX_DECODE_TIME_MS) { |
Alex Glaznev | d570484 | 2016-06-27 11:51:10 -0700 | [diff] [blame] | 821 | Logging.e(TAG, "Very high decode time: " + decodeTimeMs + "ms" |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 822 | + ". Q size: " + decodeStartTimeMs.size() |
| 823 | + ". Might be caused by resuming H264 decoding after a pause."); |
magjed | cedddbd | 2016-02-26 09:36:04 -0800 | [diff] [blame] | 824 | decodeTimeMs = MAX_DECODE_TIME_MS; |
| 825 | } |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 826 | return new DecodedOutputBuffer(result, info.offset, info.size, |
| 827 | TimeUnit.MICROSECONDS.toMillis(info.presentationTimeUs), timeStamps.timeStampMs, |
| 828 | timeStamps.ntpTimeStampMs, decodeTimeMs, SystemClock.elapsedRealtime()); |
| 829 | } |
perkj | 9cb8982 | 2015-11-11 03:27:01 -0800 | [diff] [blame] | 830 | } |
perkj | 9cb8982 | 2015-11-11 03:27:01 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 833 | // Returns null if no decoded buffer is available, and otherwise a DecodedTextureBuffer. |
| 834 | // Throws IllegalStateException if call is made on the wrong thread, if color format changes to an |
| 835 | // unsupported format, or if |mediaCodec| is not in the Executing state. Throws CodecException |
| 836 | // upon codec error. If |dequeueTimeoutMs| > 0, the oldest decoded frame will be dropped if |
| 837 | // a frame can't be returned. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 838 | @CalledByNativeUnchecked |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 839 | private @Nullable DecodedTextureBuffer dequeueTextureBuffer(int dequeueTimeoutMs) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 840 | checkOnMediaCodecThread(); |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 841 | if (!useSurface()) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 842 | throw new IllegalStateException("dequeueTexture() called for byte buffer decoding."); |
| 843 | } |
| 844 | DecodedOutputBuffer outputBuffer = dequeueOutputBuffer(dequeueTimeoutMs); |
| 845 | if (outputBuffer != null) { |
| 846 | dequeuedSurfaceOutputBuffers.add(outputBuffer); |
| 847 | } |
| 848 | |
| 849 | MaybeRenderDecodedTextureBuffer(); |
| 850 | // Check if there is texture ready now by waiting max |dequeueTimeoutMs|. |
| 851 | DecodedTextureBuffer renderedBuffer = textureListener.dequeueTextureBuffer(dequeueTimeoutMs); |
| 852 | if (renderedBuffer != null) { |
| 853 | MaybeRenderDecodedTextureBuffer(); |
| 854 | return renderedBuffer; |
| 855 | } |
| 856 | |
| 857 | if ((dequeuedSurfaceOutputBuffers.size() |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 858 | >= Math.min(MAX_QUEUED_OUTPUTBUFFERS, outputBuffers.length) |
| 859 | || (dequeueTimeoutMs > 0 && !dequeuedSurfaceOutputBuffers.isEmpty()))) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 860 | ++droppedFrames; |
| 861 | // Drop the oldest frame still in dequeuedSurfaceOutputBuffers. |
| 862 | // The oldest frame is owned by |textureListener| and can't be dropped since |
| 863 | // mediaCodec.releaseOutputBuffer has already been called. |
| 864 | final DecodedOutputBuffer droppedFrame = dequeuedSurfaceOutputBuffers.remove(); |
| 865 | if (dequeueTimeoutMs > 0) { |
perkj | 7baf79f | 2015-11-24 06:26:38 -0800 | [diff] [blame] | 866 | // TODO(perkj): Re-add the below log when VideoRenderGUI has been removed or fixed to |
| 867 | // return the one and only texture even if it does not render. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 868 | Logging.w(TAG, "Draining decoder. Dropping frame with TS: " |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 869 | + droppedFrame.presentationTimeStampMs + ". Total number of dropped frames: " |
| 870 | + droppedFrames); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 871 | } else { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 872 | Logging.w(TAG, "Too many output buffers " + dequeuedSurfaceOutputBuffers.size() |
| 873 | + ". Dropping frame with TS: " + droppedFrame.presentationTimeStampMs |
| 874 | + ". Total number of dropped frames: " + droppedFrames); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | mediaCodec.releaseOutputBuffer(droppedFrame.index, false /* render */); |
Magnus Jedvert | b9ac121 | 2018-04-23 11:29:05 +0200 | [diff] [blame] | 878 | return new DecodedTextureBuffer(null /* videoFrameBuffer */, |
| 879 | droppedFrame.presentationTimeStampMs, droppedFrame.timeStampMs, |
| 880 | droppedFrame.ntpTimeStampMs, droppedFrame.decodeTimeMs, |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 881 | SystemClock.elapsedRealtime() - droppedFrame.endDecodeTimeMs); |
| 882 | } |
| 883 | return null; |
| 884 | } |
| 885 | |
| 886 | private void MaybeRenderDecodedTextureBuffer() { |
| 887 | if (dequeuedSurfaceOutputBuffers.isEmpty() || textureListener.isWaitingForTexture()) { |
| 888 | return; |
| 889 | } |
| 890 | // Get the first frame in the queue and render to the decoder output surface. |
| 891 | final DecodedOutputBuffer buffer = dequeuedSurfaceOutputBuffers.remove(); |
| 892 | textureListener.addBufferToRender(buffer); |
| 893 | mediaCodec.releaseOutputBuffer(buffer.index, true /* render */); |
| 894 | } |
| 895 | |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 896 | // Release a dequeued output byte buffer back to the codec for re-use. Should only be called for |
| 897 | // non-surface decoding. |
| 898 | // Throws IllegalStateException if the call is made on the wrong thread, if codec is configured |
| 899 | // for surface decoding, or if |mediaCodec| is not in the Executing state. Throws |
| 900 | // MediaCodec.CodecException upon codec error. |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 901 | @CalledByNativeUnchecked |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 902 | private void returnDecodedOutputBuffer(int index) |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 903 | throws IllegalStateException, MediaCodec.CodecException { |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 904 | checkOnMediaCodecThread(); |
Magnus Jedvert | 0f0e7a6 | 2018-07-11 14:53:21 +0200 | [diff] [blame] | 905 | if (useSurface()) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 906 | throw new IllegalStateException("returnDecodedOutputBuffer() called for surface decoding."); |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 907 | } |
| 908 | mediaCodec.releaseOutputBuffer(index, false /* render */); |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 909 | } |
Magnus Jedvert | 655e196 | 2017-12-08 11:05:22 +0100 | [diff] [blame] | 910 | |
| 911 | @CalledByNative |
| 912 | ByteBuffer[] getInputBuffers() { |
| 913 | return inputBuffers; |
| 914 | } |
| 915 | |
| 916 | @CalledByNative |
| 917 | ByteBuffer[] getOutputBuffers() { |
| 918 | return outputBuffers; |
| 919 | } |
| 920 | |
| 921 | @CalledByNative |
| 922 | int getColorFormat() { |
| 923 | return colorFormat; |
| 924 | } |
| 925 | |
| 926 | @CalledByNative |
| 927 | int getWidth() { |
| 928 | return width; |
| 929 | } |
| 930 | |
| 931 | @CalledByNative |
| 932 | int getHeight() { |
| 933 | return height; |
| 934 | } |
| 935 | |
| 936 | @CalledByNative |
| 937 | int getStride() { |
| 938 | return stride; |
| 939 | } |
| 940 | |
| 941 | @CalledByNative |
| 942 | int getSliceHeight() { |
| 943 | return sliceHeight; |
| 944 | } |
glaznev@webrtc.org | efe4b9a | 2014-07-22 17:44:53 +0000 | [diff] [blame] | 945 | } |