Add TestApi to trigger the onDetect function of HotwordDetectionService

Bug: 184685043
Test: atest CtsVoiceInteractionTestCases
Test: atest CtsVoiceInteractionTestCases --instant
Change-Id: I531c1229de908c64e29f1976bd2fd1e70e545853
This commit is contained in:
lpeter
2021-04-13 02:17:46 +08:00
parent b1474958c7
commit afef0beebf
6 changed files with 222 additions and 0 deletions

View File

@@ -270,6 +270,114 @@ final class HotwordDetectionConnection {
}
}
void triggerHardwareRecognitionEventForTestLocked(
SoundTrigger.KeyphraseRecognitionEvent event,
IHotwordRecognitionStatusCallback callback) {
if (DEBUG) {
Slog.d(TAG, "triggerHardwareRecognitionEventForTestLocked");
}
detectFromDspSourceForTest(event, callback);
}
private void detectFromDspSourceForTest(SoundTrigger.KeyphraseRecognitionEvent recognitionEvent,
IHotwordRecognitionStatusCallback externalCallback) {
if (DEBUG) {
Slog.d(TAG, "detectFromDspSourceForTest");
}
AudioRecord record = createFakeAudioRecord();
if (record == null) {
Slog.d(TAG, "Failed to create fake audio record");
return;
}
Pair<ParcelFileDescriptor, ParcelFileDescriptor> clientPipe = createPipe();
if (clientPipe == null) {
Slog.d(TAG, "Failed to create pipe");
return;
}
ParcelFileDescriptor audioSink = clientPipe.second;
ParcelFileDescriptor clientRead = clientPipe.first;
record.startRecording();
mAudioCopyExecutor.execute(() -> {
try (OutputStream fos =
new ParcelFileDescriptor.AutoCloseOutputStream(audioSink)) {
int remainToRead = 10240;
byte[] buffer = new byte[1024];
while (remainToRead > 0) {
int bytesRead = record.read(buffer, 0, 1024);
if (DEBUG) {
Slog.d(TAG, "bytesRead = " + bytesRead);
}
if (bytesRead <= 0) {
break;
}
if (bytesRead > 8) {
System.arraycopy(new byte[] {'h', 'o', 't', 'w', 'o', 'r', 'd', '!'}, 0,
buffer, 0, 8);
}
fos.write(buffer, 0, bytesRead);
remainToRead -= bytesRead;
}
} catch (IOException e) {
Slog.w(TAG, "Failed supplying audio data to validator", e);
}
});
Runnable cancellingJob = () -> {
Slog.d(TAG, "Timeout for getting callback from HotwordDetectionService");
record.stop();
record.release();
bestEffortClose(audioSink);
bestEffortClose(clientRead);
};
ScheduledFuture<?> cancelingFuture =
mScheduledExecutorService.schedule(
cancellingJob, VALIDATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
IDspHotwordDetectionCallback internalCallback = new IDspHotwordDetectionCallback.Stub() {
@Override
public void onDetected(HotwordDetectedResult result) throws RemoteException {
if (DEBUG) {
Slog.d(TAG, "onDetected");
}
cancelingFuture.cancel(true);
record.stop();
record.release();
bestEffortClose(audioSink);
bestEffortClose(clientRead);
externalCallback.onKeyphraseDetected(recognitionEvent);
}
@Override
public void onRejected(HotwordRejectedResult result) throws RemoteException {
if (DEBUG) {
Slog.d(TAG, "onRejected");
}
cancelingFuture.cancel(true);
record.stop();
record.release();
bestEffortClose(audioSink);
bestEffortClose(clientRead);
externalCallback.onRejected(result);
}
};
mRemoteHotwordDetectionService.run(
service -> service.detectFromDspSource(
clientRead,
recognitionEvent.getCaptureFormat(),
VALIDATION_TIMEOUT_MILLIS,
internalCallback));
}
private void detectFromDspSource(SoundTrigger.KeyphraseRecognitionEvent recognitionEvent,
IHotwordRecognitionStatusCallback externalCallback) {
if (DEBUG) {
@@ -456,6 +564,37 @@ final class HotwordDetectionConnection {
}
}
@Nullable
private AudioRecord createFakeAudioRecord() {
if (DEBUG) {
Slog.i(TAG, "#createFakeAudioRecord");
}
try {
AudioRecord audioRecord = new AudioRecord.Builder()
.setAudioFormat(new AudioFormat.Builder()
.setSampleRate(32000)
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setChannelMask(AudioFormat.CHANNEL_IN_MONO).build())
.setAudioAttributes(new AudioAttributes.Builder()
.setInternalCapturePreset(MediaRecorder.AudioSource.HOTWORD).build())
.setBufferSizeInBytes(
AudioRecord.getMinBufferSize(32000,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT) * 2)
.build();
if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
Slog.w(TAG, "Failed to initialize AudioRecord");
audioRecord.release();
return null;
}
return audioRecord;
} catch (IllegalArgumentException e) {
Slog.e(TAG, "Failed to create AudioRecord", e);
}
return null;
}
/**
* Returns the number of bytes required to store {@code bufferLengthSeconds} of audio sampled at
* {@code sampleRate} Hz, using the format returned by DSP audio capture.

View File

@@ -1130,6 +1130,29 @@ public class VoiceInteractionManagerService extends SystemService {
}
}
@Override
public void triggerHardwareRecognitionEventForTest(
SoundTrigger.KeyphraseRecognitionEvent event,
IHotwordRecognitionStatusCallback callback)
throws RemoteException {
enforceCallingPermission(Manifest.permission.RECORD_AUDIO);
enforceCallingPermission(Manifest.permission.CAPTURE_AUDIO_HOTWORD);
synchronized (this) {
enforceIsCurrentVoiceInteractionService();
if (mImpl == null) {
Slog.w(TAG, "triggerHardwareRecognitionEventForTest without running"
+ " voice interaction service");
return;
}
final long caller = Binder.clearCallingIdentity();
try {
mImpl.triggerHardwareRecognitionEventForTestLocked(event, callback);
} finally {
Binder.restoreCallingIdentity(caller);
}
}
}
//----------------- Model management APIs --------------------------------//
@Override

View File

@@ -40,6 +40,7 @@ import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.hardware.soundtrigger.IRecognitionStatusCallback;
import android.hardware.soundtrigger.SoundTrigger;
import android.media.AudioFormat;
import android.os.Bundle;
import android.os.Handler;
@@ -493,6 +494,20 @@ class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConne
mHotwordDetectionConnection.stopListening();
}
public void triggerHardwareRecognitionEventForTestLocked(
SoundTrigger.KeyphraseRecognitionEvent event,
IHotwordRecognitionStatusCallback callback) {
if (DEBUG) {
Slog.d(TAG, "triggerHardwareRecognitionEventForTestLocked");
}
if (mHotwordDetectionConnection == null) {
Slog.w(TAG, "triggerHardwareRecognitionEventForTestLocked() called but connection"
+ " isn't established");
return;
}
mHotwordDetectionConnection.triggerHardwareRecognitionEventForTestLocked(event, callback);
}
public IRecognitionStatusCallback createSoundTriggerCallbackLocked(
IHotwordRecognitionStatusCallback callback) {
if (DEBUG) {