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