Merge pull request #3254 from TheWidlarzGroup/feat/is-codec-supported-update

feat(android): update `isCodecSupported` to return enum
This commit is contained in:
Olivier Bouillet
2023-09-29 18:28:18 +02:00
committed by GitHub
4 changed files with 37 additions and 18 deletions

View File

@@ -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");
}