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