Extract application of codec options

This will allow to reuse the same code for audio codec options.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
This commit is contained in:
Romain Vimont 2023-02-19 20:03:04 +01:00
parent 4601735e51
commit 58cf8e5401
2 changed files with 32 additions and 18 deletions

View file

@ -0,0 +1,28 @@
package com.genymobile.scrcpy;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class CodecUtils {
private CodecUtils() {
// not instantiable
}
public static void setCodecOption(MediaFormat format, String key, Object value) {
if (value instanceof Integer) {
format.setInteger(key, (Integer) value);
} else if (value instanceof Long) {
format.setLong(key, (Long) value);
} else if (value instanceof Float) {
format.setFloat(key, (Float) value);
} else if (value instanceof String) {
format.setString(key, (String) value);
}
}
}

View file

@ -237,23 +237,6 @@ public class ScreenEncoder implements Device.RotationListener {
return msg.toString(); return msg.toString();
} }
private static void setCodecOption(MediaFormat format, CodecOption codecOption) {
String key = codecOption.getKey();
Object value = codecOption.getValue();
if (value instanceof Integer) {
format.setInteger(key, (Integer) value);
} else if (value instanceof Long) {
format.setLong(key, (Long) value);
} else if (value instanceof Float) {
format.setFloat(key, (Float) value);
} else if (value instanceof String) {
format.setString(key, (String) value);
}
Ln.d("Codec option set: " + key + " (" + value.getClass().getSimpleName() + ") = " + value);
}
private static MediaFormat createFormat(String videoMimeType, int bitRate, int maxFps, List<CodecOption> codecOptions) { private static MediaFormat createFormat(String videoMimeType, int bitRate, int maxFps, List<CodecOption> codecOptions) {
MediaFormat format = new MediaFormat(); MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, videoMimeType); format.setString(MediaFormat.KEY_MIME, videoMimeType);
@ -273,7 +256,10 @@ public class ScreenEncoder implements Device.RotationListener {
if (codecOptions != null) { if (codecOptions != null) {
for (CodecOption option : codecOptions) { for (CodecOption option : codecOptions) {
setCodecOption(format, option); String key = option.getKey();
Object value = option.getValue();
CodecUtils.setCodecOption(format, key, value);
Ln.d("Video codec option set: " + key + " (" + value.getClass().getSimpleName() + ") = " + value);
} }
} }