From 5474ae6bd62f65e6aa25a8685a7e5db9571390ac Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 24 Oct 2024 23:50:54 +0200 Subject: [PATCH] Factorize codec info listing Make the listing of video and audio encoders share the same code. PR #5416 --- .../genymobile/scrcpy/util/CodecUtils.java | 45 +------------------ .../com/genymobile/scrcpy/util/LogUtils.java | 38 ++++++++-------- 2 files changed, 19 insertions(+), 64 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/util/CodecUtils.java b/server/src/main/java/com/genymobile/scrcpy/util/CodecUtils.java index 5b0c95e8..3a01256a 100644 --- a/server/src/main/java/com/genymobile/scrcpy/util/CodecUtils.java +++ b/server/src/main/java/com/genymobile/scrcpy/util/CodecUtils.java @@ -1,8 +1,5 @@ package com.genymobile.scrcpy.util; -import com.genymobile.scrcpy.audio.AudioCodec; -import com.genymobile.scrcpy.video.VideoCodec; - import android.media.MediaCodecInfo; import android.media.MediaCodecList; import android.media.MediaFormat; @@ -13,24 +10,6 @@ import java.util.List; public final class CodecUtils { - public static final class DeviceEncoder { - private final Codec codec; - private final MediaCodecInfo info; - - DeviceEncoder(Codec codec, MediaCodecInfo info) { - this.codec = codec; - this.info = info; - } - - public Codec getCodec() { - return codec; - } - - public MediaCodecInfo getInfo() { - return info; - } - } - private CodecUtils() { // not instantiable } @@ -47,7 +26,7 @@ public final class CodecUtils { } } - private static MediaCodecInfo[] getEncoders(MediaCodecList codecs, String mimeType) { + public static MediaCodecInfo[] getEncoders(MediaCodecList codecs, String mimeType) { List result = new ArrayList<>(); for (MediaCodecInfo codecInfo : codecs.getCodecInfos()) { if (codecInfo.isEncoder() && Arrays.asList(codecInfo.getSupportedTypes()).contains(mimeType)) { @@ -56,26 +35,4 @@ public final class CodecUtils { } return result.toArray(new MediaCodecInfo[result.size()]); } - - public static List listVideoEncoders() { - List encoders = new ArrayList<>(); - MediaCodecList codecs = new MediaCodecList(MediaCodecList.REGULAR_CODECS); - for (VideoCodec codec : VideoCodec.values()) { - for (MediaCodecInfo info : getEncoders(codecs, codec.getMimeType())) { - encoders.add(new DeviceEncoder(codec, info)); - } - } - return encoders; - } - - public static List listAudioEncoders() { - List encoders = new ArrayList<>(); - MediaCodecList codecs = new MediaCodecList(MediaCodecList.REGULAR_CODECS); - for (AudioCodec codec : AudioCodec.values()) { - for (MediaCodecInfo info : getEncoders(codecs, codec.getMimeType())) { - encoders.add(new DeviceEncoder(codec, info)); - } - } - return encoders; - } } diff --git a/server/src/main/java/com/genymobile/scrcpy/util/LogUtils.java b/server/src/main/java/com/genymobile/scrcpy/util/LogUtils.java index 6b813135..f2837f40 100644 --- a/server/src/main/java/com/genymobile/scrcpy/util/LogUtils.java +++ b/server/src/main/java/com/genymobile/scrcpy/util/LogUtils.java @@ -1,9 +1,11 @@ package com.genymobile.scrcpy.util; +import com.genymobile.scrcpy.audio.AudioCodec; import com.genymobile.scrcpy.device.Device; import com.genymobile.scrcpy.device.DeviceApp; import com.genymobile.scrcpy.device.DisplayInfo; import com.genymobile.scrcpy.device.Size; +import com.genymobile.scrcpy.video.VideoCodec; import com.genymobile.scrcpy.wrappers.DisplayManager; import com.genymobile.scrcpy.wrappers.ServiceManager; @@ -14,6 +16,8 @@ import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraManager; import android.hardware.camera2.params.StreamConfigurationMap; import android.media.MediaCodec; +import android.media.MediaCodecInfo; +import android.media.MediaCodecList; import android.util.Range; import java.util.Collections; @@ -28,32 +32,26 @@ public final class LogUtils { // not instantiable } - public static String buildVideoEncoderListMessage() { - StringBuilder builder = new StringBuilder("List of video encoders:"); - List videoEncoders = CodecUtils.listVideoEncoders(); - if (videoEncoders.isEmpty()) { - builder.append("\n (none)"); - } else { - for (CodecUtils.DeviceEncoder encoder : videoEncoders) { - builder.append("\n --video-codec=").append(encoder.getCodec().getName()); - builder.append(" --video-encoder=").append(encoder.getInfo().getName()); + private static String buildEncoderListMessage(String type, Codec[] codecs) { + StringBuilder builder = new StringBuilder("List of ").append(type).append(" encoders:"); + MediaCodecList codecList = new MediaCodecList(MediaCodecList.REGULAR_CODECS); + for (Codec codec : codecs) { + MediaCodecInfo[] encoders = CodecUtils.getEncoders(codecList, codec.getMimeType()); + for (MediaCodecInfo info : encoders) { + builder.append("\n --").append(type).append("-codec=").append(codec.getName()); + builder.append(" --").append(type).append("-encoder=").append(info.getName()); } } + return builder.toString(); } + public static String buildVideoEncoderListMessage() { + return buildEncoderListMessage("video", VideoCodec.values()); + } + public static String buildAudioEncoderListMessage() { - StringBuilder builder = new StringBuilder("List of audio encoders:"); - List audioEncoders = CodecUtils.listAudioEncoders(); - if (audioEncoders.isEmpty()) { - builder.append("\n (none)"); - } else { - for (CodecUtils.DeviceEncoder encoder : audioEncoders) { - builder.append("\n --audio-codec=").append(encoder.getCodec().getName()); - builder.append(" --audio-encoder=").append(encoder.getInfo().getName()); - } - } - return builder.toString(); + return buildEncoderListMessage("audio", AudioCodec.values()); } public static String buildDisplayListMessage() {