Implement equals() and hashCode() for VideoCodecInfo.

To be able to compare VideoCodecInfos in a nice way in Java and still
use the correct criteria for comparing H264 codec infos.

A similar thing was done for Obj-C here:
https://webrtc-review.googlesource.com/c/src/+/4383

Bug: webrtc:7925
Change-Id: I43f532d4efa557fc8fe25a82eebc35072b91e6db
Reviewed-on: https://webrtc-review.googlesource.com/23240
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Commit-Queue: Anders Carlsson <andersc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20716}
This commit is contained in:
Anders Carlsson
2017-11-16 15:44:14 +01:00
committed by Commit Bot
parent 4d85e8a78c
commit 1e1dd77604
5 changed files with 47 additions and 42 deletions

View File

@ -10,8 +10,8 @@
package org.webrtc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
public class DefaultVideoEncoderFactory implements VideoEncoderFactory {
@ -33,38 +33,20 @@ public class DefaultVideoEncoderFactory implements VideoEncoderFactory {
@Override
public VideoEncoder createEncoder(VideoCodecInfo info) {
List<VideoCodecInfo> hardwareSupportedCodecs =
Arrays.asList(hardwareVideoEncoderFactory.getSupportedCodecs());
if (containsSameCodec(hardwareSupportedCodecs, info)) {
return hardwareVideoEncoderFactory.createEncoder(info);
} else {
return softwareVideoEncoderFactory.createEncoder(info);
final VideoEncoder videoEncoder = hardwareVideoEncoderFactory.createEncoder(info);
if (videoEncoder != null) {
return videoEncoder;
}
return softwareVideoEncoderFactory.createEncoder(info);
}
@Override
public VideoCodecInfo[] getSupportedCodecs() {
List<VideoCodecInfo> supportedCodecInfos = new ArrayList<>();
LinkedHashSet<VideoCodecInfo> supportedCodecInfos = new LinkedHashSet<VideoCodecInfo>();
supportedCodecInfos.addAll(Arrays.asList(softwareVideoEncoderFactory.getSupportedCodecs()));
for (VideoCodecInfo info : hardwareVideoEncoderFactory.getSupportedCodecs()) {
if (!containsSameCodec(supportedCodecInfos, info)) {
supportedCodecInfos.add(info);
}
}
supportedCodecInfos.addAll(Arrays.asList(hardwareVideoEncoderFactory.getSupportedCodecs()));
return supportedCodecInfos.toArray(new VideoCodecInfo[supportedCodecInfos.size()]);
}
private static boolean containsSameCodec(List<VideoCodecInfo> infos, VideoCodecInfo info) {
for (VideoCodecInfo otherInfo : infos) {
if (isSameCodec(info, otherInfo)) {
return true;
}
}
return false;
}
private static native boolean isSameCodec(VideoCodecInfo info1, VideoCodecInfo info2);
}

View File

@ -93,6 +93,16 @@ public class HardwareVideoEncoderFactory implements VideoEncoderFactory {
Integer yuvColorFormat = MediaCodecUtils.selectColorFormat(
MediaCodecUtils.ENCODER_COLOR_FORMATS, info.getCapabilitiesForType(mime));
if (type == VideoCodecType.H264) {
boolean isHighProfile = isSameH264Profile(input.params, getCodecProperties(type, true))
&& isH264HighProfileSupported(info);
boolean isBaselineProfile = isSameH264Profile(input.params, getCodecProperties(type, false));
if (!isHighProfile && !isBaselineProfile) {
return null;
}
}
return new HardwareVideoEncoder(codecName, type, surfaceColorFormat, yuvColorFormat,
input.params, getKeyFrameIntervalSec(type), getForcedKeyFrameIntervalMs(type, codecName),
createBitrateAdjuster(type, codecName), sharedContext);
@ -269,4 +279,7 @@ public class HardwareVideoEncoderFactory implements VideoEncoderFactory {
throw new IllegalArgumentException("Unsupported codec: " + type);
}
}
private static native boolean isSameH264Profile(
Map<String, String> params1, Map<String, String> params2);
}

View File

@ -10,6 +10,8 @@
package org.webrtc;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
/**
@ -45,4 +47,23 @@ public class VideoCodecInfo {
this.name = name;
this.params = params;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof VideoCodecInfo))
return false;
VideoCodecInfo otherInfo = (VideoCodecInfo) obj;
return name.equalsIgnoreCase(otherInfo.name) && params.equals(otherInfo.params);
}
@Override
public int hashCode() {
Object[] values = {name.toUpperCase(Locale.ROOT), params};
return Arrays.hashCode(values);
}
}