feat(android): update isCodecSupported to return enum

This commit is contained in:
KrzysztofMoch 2023-09-25 08:17:03 +02:00
parent 01a47840be
commit 812e9dc84f

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