Rename audio capture exception

The AudioCaptureForegroundException was very specific. Rename it to
AudioCaptureException to support other capture failures.

PR #5102 <https://github.com/Genymobile/scrcpy/pull/5102>
This commit is contained in:
Romain Vimont 2024-07-14 23:08:45 +02:00
parent 39132ff2dd
commit 3b8ec0c38d
5 changed files with 19 additions and 14 deletions

View File

@ -89,7 +89,7 @@ public final class AudioCapture {
ServiceManager.getActivityManager().forceStopPackage(FakeContext.PACKAGE_NAME); ServiceManager.getActivityManager().forceStopPackage(FakeContext.PACKAGE_NAME);
} }
private void tryStartRecording(int attempts, int delayMs) throws AudioCaptureForegroundException { private void tryStartRecording(int attempts, int delayMs) throws AudioCaptureException {
while (attempts-- > 0) { while (attempts-- > 0) {
// Wait for activity to start // Wait for activity to start
SystemClock.sleep(delayMs); SystemClock.sleep(delayMs);
@ -101,7 +101,7 @@ public final class AudioCapture {
Ln.e("Failed to start audio capture"); Ln.e("Failed to start audio capture");
Ln.e("On Android 11, audio capture must be started in the foreground, make sure that the device is unlocked when starting " Ln.e("On Android 11, audio capture must be started in the foreground, make sure that the device is unlocked when starting "
+ "scrcpy."); + "scrcpy.");
throw new AudioCaptureForegroundException(); throw new AudioCaptureException();
} else { } else {
Ln.d("Failed to start audio capture, retrying..."); Ln.d("Failed to start audio capture, retrying...");
} }
@ -121,7 +121,7 @@ public final class AudioCapture {
recorder.startRecording(); recorder.startRecording();
} }
public void start() throws AudioCaptureForegroundException { public void start() throws AudioCaptureException {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
startWorkaroundAndroid11(); startWorkaroundAndroid11();
try { try {

View File

@ -0,0 +1,12 @@
package com.genymobile.scrcpy.audio;
/**
* Exception for any audio capture issue.
* <p/>
* This includes the case where audio capture failed on Android 11 specifically because the running App (Shell) was not in foreground.
* <p/>
* Its purpose is to disable audio without errors (that's why the exception is empty, any error message must be printed by the caller before
* throwing the exception).
*/
public class AudioCaptureException extends Exception {
}

View File

@ -1,7 +0,0 @@
package com.genymobile.scrcpy.audio;
/**
* Exception thrown if audio capture failed on Android 11 specifically because the running App (shell) was not in foreground.
*/
public class AudioCaptureForegroundException extends Exception {
}

View File

@ -132,7 +132,7 @@ public final class AudioEncoder implements AsyncProcessor {
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
// Do not print stack trace, a user-friendly error-message has already been logged // Do not print stack trace, a user-friendly error-message has already been logged
fatalError = true; fatalError = true;
} catch (AudioCaptureForegroundException e) { } catch (AudioCaptureException e) {
// Do not print stack trace, a user-friendly error-message has already been logged // Do not print stack trace, a user-friendly error-message has already been logged
} catch (IOException e) { } catch (IOException e) {
Ln.e("Audio encoding error", e); Ln.e("Audio encoding error", e);
@ -176,7 +176,7 @@ public final class AudioEncoder implements AsyncProcessor {
} }
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
private void encode() throws IOException, ConfigurationException, AudioCaptureForegroundException { private void encode() throws IOException, ConfigurationException, AudioCaptureException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Ln.w("Audio disabled: it is not supported before Android 11"); Ln.w("Audio disabled: it is not supported before Android 11");
streamer.writeDisableStream(false); streamer.writeDisableStream(false);

View File

@ -23,7 +23,7 @@ public final class AudioRawRecorder implements AsyncProcessor {
this.streamer = streamer; this.streamer = streamer;
} }
private void record() throws IOException, AudioCaptureForegroundException { private void record() throws IOException, AudioCaptureException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Ln.w("Audio disabled: it is not supported before Android 11"); Ln.w("Audio disabled: it is not supported before Android 11");
streamer.writeDisableStream(false); streamer.writeDisableStream(false);
@ -69,7 +69,7 @@ public final class AudioRawRecorder implements AsyncProcessor {
boolean fatalError = false; boolean fatalError = false;
try { try {
record(); record();
} catch (AudioCaptureForegroundException e) { } catch (AudioCaptureException e) {
// Do not print stack trace, a user-friendly error-message has already been logged // Do not print stack trace, a user-friendly error-message has already been logged
} catch (Throwable t) { } catch (Throwable t) {
Ln.e("Audio recording error", t); Ln.e("Audio recording error", t);