Skip to content

Commit

Permalink
Rename audio capture exception
Browse files Browse the repository at this point in the history
The AudioCaptureForegroundException was very specific. Rename it to
AudioCaptureException to support other capture failures.

PR Genymobile#5102 <Genymobile#5102>
  • Loading branch information
rom1v authored and Gottox committed Sep 29, 2024
1 parent 6c3b1ae commit 8a81dcc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static void stopWorkaroundAndroid11() {
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) {
// Wait for activity to start
SystemClock.sleep(delayMs);
Expand All @@ -101,7 +101,7 @@ private void tryStartRecording(int attempts, int delayMs) throws AudioCaptureFor
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 "
+ "scrcpy.");
throw new AudioCaptureForegroundException();
throw new AudioCaptureException();
} else {
Ln.d("Failed to start audio capture, retrying...");
}
Expand All @@ -121,7 +121,7 @@ private void startRecording() {
recorder.startRecording();
}

public void start() throws AudioCaptureForegroundException {
public void start() throws AudioCaptureException {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
startWorkaroundAndroid11();
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void start(TerminationListener listener) {
} catch (ConfigurationException e) {
// Do not print stack trace, a user-friendly error-message has already been logged
fatalError = true;
} catch (AudioCaptureForegroundException e) {
} catch (AudioCaptureException e) {
// Do not print stack trace, a user-friendly error-message has already been logged
} catch (IOException e) {
Ln.e("Audio encoding error", e);
Expand Down Expand Up @@ -176,7 +176,7 @@ private synchronized void waitEnded() {
}

@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) {
Ln.w("Audio disabled: it is not supported before Android 11");
streamer.writeDisableStream(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public AudioRawRecorder(AudioCapture capture, 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) {
Ln.w("Audio disabled: it is not supported before Android 11");
streamer.writeDisableStream(false);
Expand Down Expand Up @@ -69,7 +69,7 @@ public void start(TerminationListener listener) {
boolean fatalError = false;
try {
record();
} catch (AudioCaptureForegroundException e) {
} catch (AudioCaptureException e) {
// Do not print stack trace, a user-friendly error-message has already been logged
} catch (Throwable t) {
Ln.e("Audio recording error", t);
Expand Down

0 comments on commit 8a81dcc

Please sign in to comment.