From 812e9dc84fd8bed2ab8e46883fc0366c9d6ed8c9 Mon Sep 17 00:00:00 2001 From: KrzysztofMoch Date: Mon, 25 Sep 2023 08:17:03 +0200 Subject: [PATCH] feat(android): update `isCodecSupported` to return enum --- .../react/VideoDecoderPropertiesModule.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/brentvatne/react/VideoDecoderPropertiesModule.java b/android/src/main/java/com/brentvatne/react/VideoDecoderPropertiesModule.java index 6622e7f9..f6340c32 100644 --- a/android/src/main/java/com/brentvatne/react/VideoDecoderPropertiesModule.java +++ b/android/src/main/java/com/brentvatne/react/VideoDecoderPropertiesModule.java @@ -1,6 +1,7 @@ package com.brentvatne.react; import android.annotation.SuppressLint; +import android.media.MediaCodecInfo; import android.media.MediaCodecList; import android.media.MediaDrm; import android.media.MediaFormat; @@ -79,18 +80,36 @@ public class VideoDecoderPropertiesModule extends ReactContextBaseJavaModule { @SuppressLint("ObsoleteSdkInt") @ReactMethod public void isCodecSupported(String mimeType, int width, int height, Promise p) { - if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { - p.resolve(false); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + p.resolve("unsupported"); return; } + MediaCodecList mRegularCodecs = new MediaCodecList(MediaCodecList.REGULAR_CODECS); MediaFormat format = MediaFormat.createVideoFormat(mimeType, width, height); String codecName = mRegularCodecs.findDecoderForFormat(format); + if (codecName == null) { - p.resolve(false); - } else { - p.resolve(true); + p.resolve("unsupported"); + return; } + + // Fallback for android < 10 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + p.resolve("software"); + return; + } + + boolean isHardwareAccelerated = false; + + for (MediaCodecInfo codecInfo : mRegularCodecs.getCodecInfos()) { + if (codecInfo.getName().equalsIgnoreCase(codecName)) { + isHardwareAccelerated = codecInfo.isHardwareAccelerated(); + break; + } + } + + p.resolve(isHardwareAccelerated ? "software" : "hardware"); }