From 6f332a2bc73853884c894ee2400ad9c92aabc34c Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 19 Feb 2023 20:03:04 +0100 Subject: [PATCH] Extract unknown encoder error message This will allow to reuse the same code for audio encoder selection. PR #3757 --- .../com/genymobile/scrcpy/CodecUtils.java | 25 +++++++++++++++++ .../com/genymobile/scrcpy/ScreenEncoder.java | 28 +------------------ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/CodecUtils.java b/server/src/main/java/com/genymobile/scrcpy/CodecUtils.java index 2a808c59..96887c14 100644 --- a/server/src/main/java/com/genymobile/scrcpy/CodecUtils.java +++ b/server/src/main/java/com/genymobile/scrcpy/CodecUtils.java @@ -25,4 +25,29 @@ public final class CodecUtils { format.setString(key, (String) value); } } + + public static String buildUnknownEncoderMessage(Codec codec, String encoderName) { + StringBuilder msg = new StringBuilder("Encoder '").append(encoderName).append("' for ").append(codec.getName()).append(" not found"); + MediaCodecInfo[] encoders = listEncoders(codec.getMimeType()); + if (encoders != null && encoders.length > 0) { + msg.append("\nTry to use one of the available encoders:"); + String codecOption = codec.getType() == Codec.Type.VIDEO ? "video-codec" : "audio-codec"; + for (MediaCodecInfo encoder : encoders) { + msg.append("\n scrcpy --").append(codecOption).append("=").append(codec.getName()); + msg.append(" --encoder='").append(encoder.getName()).append("'"); + } + } + return msg.toString(); + } + + private static MediaCodecInfo[] listEncoders(String mimeType) { + List result = new ArrayList<>(); + MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS); + for (MediaCodecInfo codecInfo : list.getCodecInfos()) { + if (codecInfo.isEncoder() && Arrays.asList(codecInfo.getSupportedTypes()).contains(mimeType)) { + result.add(codecInfo); + } + } + return result.toArray(new MediaCodecInfo[result.size()]); + } } diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index 1c3ccf72..77cd1de4 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -5,7 +5,6 @@ import com.genymobile.scrcpy.wrappers.SurfaceControl; import android.graphics.Rect; import android.media.MediaCodec; import android.media.MediaCodecInfo; -import android.media.MediaCodecList; import android.media.MediaFormat; import android.os.Build; import android.os.IBinder; @@ -14,8 +13,6 @@ import android.view.Surface; import java.io.IOException; import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; @@ -199,24 +196,13 @@ public class ScreenEncoder implements Device.RotationListener { return !eof; } - private static MediaCodecInfo[] listEncoders(String videoMimeType) { - List result = new ArrayList<>(); - MediaCodecList list = new MediaCodecList(MediaCodecList.REGULAR_CODECS); - for (MediaCodecInfo codecInfo : list.getCodecInfos()) { - if (codecInfo.isEncoder() && Arrays.asList(codecInfo.getSupportedTypes()).contains(videoMimeType)) { - result.add(codecInfo); - } - } - return result.toArray(new MediaCodecInfo[result.size()]); - } - private static MediaCodec createMediaCodec(Codec codec, String encoderName) throws IOException, ConfigurationException { if (encoderName != null) { Ln.d("Creating encoder by name: '" + encoderName + "'"); try { return MediaCodec.createByCodecName(encoderName); } catch (IllegalArgumentException e) { - Ln.e(buildUnknownEncoderMessage(codec, encoderName)); + Ln.e(CodecUtils.buildUnknownEncoderMessage(codec, encoderName)); throw new ConfigurationException("Unknown encoder: " + encoderName); } } @@ -225,18 +211,6 @@ public class ScreenEncoder implements Device.RotationListener { return mediaCodec; } - private static String buildUnknownEncoderMessage(Codec codec, String encoderName) { - StringBuilder msg = new StringBuilder("Encoder '").append(encoderName).append("' for ").append(codec.getName()).append(" not found"); - MediaCodecInfo[] encoders = listEncoders(codec.getMimeType()); - if (encoders != null && encoders.length > 0) { - msg.append("\nTry to use one of the available encoders:"); - for (MediaCodecInfo encoder : encoders) { - msg.append("\n scrcpy --video-codec=").append(codec.getName()).append(" --encoder='").append(encoder.getName()).append("'"); - } - } - return msg.toString(); - } - private static MediaFormat createFormat(String videoMimeType, int bitRate, int maxFps, List codecOptions) { MediaFormat format = new MediaFormat(); format.setString(MediaFormat.KEY_MIME, videoMimeType);