diff options
author | Christian Frank <chfrank@google.com> | 2021-07-06 09:25:35 +0200 |
---|---|---|
committer | Christian Frank <chfrank@google.com> | 2021-07-08 05:54:09 +0000 |
commit | 69ea5c8016083332a1e4b7f78fe5f2c77e2c879f (patch) | |
tree | 03134d9c1585a7d5680d1bf40f6f8a8dbe102360 | |
parent | c5532b030c053fb0f6d564b45ecf260854107eec (diff) |
Address corner case in which empty audio is requested and MusicRecognitionManagerService crashes.
Behavior before this change would turn on mic indicator and then crash
(leaving the indicator on indefinitely).
Second use case: Allow clients of the API to invoke the service without
actually streaming audio. For context see:
https://b.corp.google.com/issues/184808866#comment11
BUG: 192938158
BUG: 184808866
Change-Id: I17d1b7e1a489262a034d333fc42fb1dcd45c48c1
-rw-r--r-- | services/musicrecognition/java/com/android/server/musicrecognition/MusicRecognitionManagerPerUserService.java | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/services/musicrecognition/java/com/android/server/musicrecognition/MusicRecognitionManagerPerUserService.java b/services/musicrecognition/java/com/android/server/musicrecognition/MusicRecognitionManagerPerUserService.java index 4c5bbebdfd45..2cd20c56b5e1 100644 --- a/services/musicrecognition/java/com/android/server/musicrecognition/MusicRecognitionManagerPerUserService.java +++ b/services/musicrecognition/java/com/android/server/musicrecognition/MusicRecognitionManagerPerUserService.java @@ -208,6 +208,24 @@ public final class MusicRecognitionManagerPerUserService extends @NonNull RecognitionRequest recognitionRequest, IMusicRecognitionManagerCallback clientCallback, ParcelFileDescriptor audioSink) { + int maxAudioLengthSeconds = Math.min(recognitionRequest.getMaxAudioLengthSeconds(), + MAX_STREAMING_SECONDS); + if (maxAudioLengthSeconds <= 0) { + // TODO(b/192992319): A request to stream 0s of audio can be used to initialize the + // music recognition service implementation, hence not reporting an error here. + // The TODO for Android T is to move this functionality into an init() API call. + Slog.i(TAG, "No audio requested. Closing stream."); + try { + audioSink.close(); + clientCallback.onAudioStreamClosed(); + } catch (IOException e) { + Slog.e(TAG, "Problem closing stream.", e); + } catch (RemoteException ignored) { + // Ignored. + } + return; + } + try { startRecordAudioOp(attributionTag); } catch (SecurityException e) { @@ -224,8 +242,6 @@ public final class MusicRecognitionManagerPerUserService extends return; } - int maxAudioLengthSeconds = Math.min(recognitionRequest.getMaxAudioLengthSeconds(), - MAX_STREAMING_SECONDS); AudioRecord audioRecord = createAudioRecord(recognitionRequest, maxAudioLengthSeconds); try (OutputStream fos = new ParcelFileDescriptor.AutoCloseOutputStream(audioSink)) { |