Skip to content

Commit

Permalink
add DefaultBlacklistedVideoDecoderFactory (#40)
Browse files Browse the repository at this point in the history
* add DefaultBlacklistedVideoDecoderFactory

* use decoder.implementationName

* fix compilation

* fix logging

* fix logging func call

* fix imports + package
  • Loading branch information
kanat authored and santhoshvai committed Nov 20, 2024
1 parent 332cb33 commit 94f79ec
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ if (is_android) {
"api/org/webrtc/DefaultVideoDecoderFactory.java",
"api/org/webrtc/DefaultVideoEncoderFactory.java",
"api/org/webrtc/WrappedVideoDecoderFactory.java",
"api/org/webrtc/DefaultBlacklistedVideoDecoderFactory.java",
"api/org/webrtc/DefaultAlignedVideoEncoderFactory.java",
"api/org/webrtc/SimulcastAlignedVideoEncoderFactory.java",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.webrtc;

import androidx.annotation.Nullable;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;

public class DefaultBlacklistedVideoDecoderFactory implements VideoDecoderFactory {

private static final String TAG = "DefaultBlacklistedVideoDecoderFactory";

private static final Predicate<VideoDecoder> defaultBlacklistedPredicate =
new Predicate<VideoDecoder>() {
@Override
public boolean test(@Nullable VideoDecoder decoder) {
// if the decoder is Exynos VP9, then blacklist it
return isExynosVP9(decoder);
}
};

private final VideoDecoderFactory hardwareVideoDecoderFactory;
private final VideoDecoderFactory softwareVideoDecoderFactory;
private final VideoDecoderFactory platformSoftwareVideoDecoderFactory;
private final Predicate<VideoDecoder> isHardwareDecoderBlacklisted;

public DefaultBlacklistedVideoDecoderFactory(@Nullable EglBase.Context eglContext) {
this(eglContext, null);
}

public DefaultBlacklistedVideoDecoderFactory(
@Nullable EglBase.Context eglContext,
@Nullable Predicate<VideoDecoder> decoderBlacklistedPredicate) {
this.hardwareVideoDecoderFactory = new HardwareVideoDecoderFactory(eglContext);
this.softwareVideoDecoderFactory = new SoftwareVideoDecoderFactory();
this.platformSoftwareVideoDecoderFactory = new PlatformSoftwareVideoDecoderFactory(eglContext);
this.isHardwareDecoderBlacklisted = decoderBlacklistedPredicate == null
? defaultBlacklistedPredicate
: decoderBlacklistedPredicate.or(defaultBlacklistedPredicate);
}

@Override
public VideoDecoder createDecoder(VideoCodecInfo codecType) {
VideoDecoder softwareDecoder = softwareVideoDecoderFactory.createDecoder(codecType);
VideoDecoder hardwareDecoder = hardwareVideoDecoderFactory.createDecoder(codecType);
if (softwareDecoder == null) {
softwareDecoder = platformSoftwareVideoDecoderFactory.createDecoder(codecType);
}

if (isHardwareDecoderBlacklisted.test(hardwareDecoder)) {
Logging.d(TAG, "Hardware decoder is blacklisted: " + hardwareDecoder.getImplementationName());
return softwareDecoder;
}

if (hardwareDecoder != null && softwareDecoder != null) {
return new VideoDecoderFallback(softwareDecoder, hardwareDecoder);
} else {
return hardwareDecoder != null ? hardwareDecoder : softwareDecoder;
}
}

@Override
public VideoCodecInfo[] getSupportedCodecs() {
Set<VideoCodecInfo> supportedCodecInfos = new HashSet<>();
supportedCodecInfos.addAll(Arrays.asList(softwareVideoDecoderFactory.getSupportedCodecs()));
supportedCodecInfos.addAll(Arrays.asList(hardwareVideoDecoderFactory.getSupportedCodecs()));
supportedCodecInfos.addAll(Arrays.asList(platformSoftwareVideoDecoderFactory.getSupportedCodecs()));
return supportedCodecInfos.toArray(new VideoCodecInfo[0]);
}

private static boolean isExynosVP9(@Nullable VideoDecoder decoder) {
if (decoder == null) {
return false;
}
final String name = decoder.getImplementationName().toLowerCase();
return name.contains("exynos") && name.contains("vp9");
}
}

0 comments on commit 94f79ec

Please sign in to comment.