Do not print stacktraces when unnecessary

User-friendly error messages are printed on specific configuration
exceptions. In that case, do not print the stacktrace.

Also handle the user-friendly error message directly where the error
occurs, and print multiline messages in a single log call, to avoid
confusing interleaving.
This commit is contained in:
Romain Vimont
2023-02-19 19:36:46 +01:00
parent a51e500584
commit 6840a6939a
6 changed files with 44 additions and 77 deletions

View File

@@ -63,7 +63,7 @@ public class ScreenEncoder implements Device.RotationListener {
return rotationChanged.getAndSet(false);
}
public void streamScreen(Device device, Callbacks callbacks) throws IOException {
public void streamScreen(Device device, Callbacks callbacks) throws IOException, ConfigurationException {
MediaCodec codec = createCodec(videoMimeType, encoderName);
MediaFormat format = createFormat(videoMimeType, bitRate, maxFps, codecOptions);
IBinder display = createDisplay();
@@ -207,14 +207,14 @@ public class ScreenEncoder implements Device.RotationListener {
return result.toArray(new MediaCodecInfo[result.size()]);
}
private static MediaCodec createCodec(String videoMimeType, String encoderName) throws IOException {
private static MediaCodec createCodec(String videoMimeType, String encoderName) throws IOException, ConfigurationException {
if (encoderName != null) {
Ln.d("Creating encoder by name: '" + encoderName + "'");
try {
return MediaCodec.createByCodecName(encoderName);
} catch (IllegalArgumentException e) {
MediaCodecInfo[] encoders = listEncoders(videoMimeType);
throw new InvalidEncoderException(encoderName, encoders);
Ln.e(buildUnknownEncoderMessage(videoMimeType, encoderName));
throw new ConfigurationException("Unknown encoder: " + encoderName);
}
}
MediaCodec codec = MediaCodec.createEncoderByType(videoMimeType);
@@ -222,6 +222,18 @@ public class ScreenEncoder implements Device.RotationListener {
return codec;
}
private static String buildUnknownEncoderMessage(String videoMimeType, String encoderName) {
StringBuilder msg = new StringBuilder("Encoder '").append(encoderName).append("' not found");
MediaCodecInfo[] encoders = listEncoders(videoMimeType);
if (encoders != null && encoders.length > 0) {
msg.append("\nTry to use one of the available encoders:");
for (MediaCodecInfo encoder : encoders) {
msg.append("\n scrcpy --encoder='").append(encoder.getName()).append("'");
}
}
return msg.toString();
}
private static void setCodecOption(MediaFormat format, CodecOption codecOption) {
String key = codecOption.getKey();
Object value = codecOption.getValue();