Search in sources :

Example 1 with RecognitionConfig

use of android.hardware.soundtrigger.SoundTrigger.RecognitionConfig in project platform_frameworks_base by android.

the class GenericSoundModelTest method testFuzzGenericSoundModel.

/**
     * Tests a more complicated pattern of loading, unloading, triggering, starting and stopping
     * recognition. Intended to find unexpected errors that occur in unexpected states.
     */
@LargeTest
public void testFuzzGenericSoundModel() throws Exception {
    int numModels = 2;
    final int STATUS_UNLOADED = 0;
    final int STATUS_LOADED = 1;
    final int STATUS_STARTED = 2;
    class ModelInfo {

        int status;

        GenericSoundModel model;

        public ModelInfo(GenericSoundModel model, int status) {
            this.status = status;
            this.model = model;
        }
    }
    Random predictableRandom = new Random(100);
    ArrayList modelInfos = new ArrayList<ModelInfo>();
    for (int i = 0; i < numModels; i++) {
        // Create sound model
        byte[] data = new byte[1024];
        predictableRandom.nextBytes(data);
        UUID modelUuid = UUID.randomUUID();
        UUID mVendorUuid = UUID.randomUUID();
        GenericSoundModel model = new GenericSoundModel(modelUuid, mVendorUuid, data);
        ModelInfo modelInfo = new ModelInfo(model, STATUS_UNLOADED);
        modelInfos.add(modelInfo);
    }
    boolean captureTriggerAudio = true;
    boolean allowMultipleTriggers = true;
    RecognitionConfig config = new RecognitionConfig(captureTriggerAudio, allowMultipleTriggers, null, null);
    TestRecognitionStatusCallback spyCallback = spy(new TestRecognitionStatusCallback());
    int numOperationsToRun = 100;
    for (int i = 0; i < numOperationsToRun; i++) {
        // Select a random model
        int modelInfoIndex = predictableRandom.nextInt(modelInfos.size());
        ModelInfo modelInfo = (ModelInfo) modelInfos.get(modelInfoIndex);
        // Perform a random operation
        int operation = predictableRandom.nextInt(5);
        if (operation == 0 && modelInfo.status == STATUS_UNLOADED) {
            // Update and start sound model
            soundTriggerService.updateSoundModel(modelInfo.model);
            loadedModelUuids.add(modelInfo.model.uuid);
            modelInfo.status = STATUS_LOADED;
        } else if (operation == 1 && modelInfo.status == STATUS_LOADED) {
            // Start the sound model
            int r = soundTriggerService.startRecognition(new ParcelUuid(modelInfo.model.uuid), spyCallback, config);
            assertEquals("Could Not Start Recognition with code: " + r, android.hardware.soundtrigger.SoundTrigger.STATUS_OK, r);
            modelInfo.status = STATUS_STARTED;
        } else if (operation == 2 && modelInfo.status == STATUS_STARTED) {
            // Send trigger to stub HAL
            Socket socket = new Socket(InetAddress.getLocalHost(), 14035);
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.writeBytes("trig " + modelInfo.model.uuid + "\r\n");
            out.flush();
            socket.close();
            // Verify trigger was received
            verify(spyCallback, timeout(100)).onGenericSoundTriggerDetected(any());
            reset(spyCallback);
        } else if (operation == 3 && modelInfo.status == STATUS_STARTED) {
            // Stop recognition
            int r = soundTriggerService.stopRecognition(new ParcelUuid(modelInfo.model.uuid), spyCallback);
            assertEquals("Could Not Stop Recognition with code: " + r, android.hardware.soundtrigger.SoundTrigger.STATUS_OK, r);
            modelInfo.status = STATUS_LOADED;
        } else if (operation == 4 && modelInfo.status != STATUS_UNLOADED) {
            // Delete sound model
            soundTriggerService.deleteSoundModel(new ParcelUuid(modelInfo.model.uuid));
            loadedModelUuids.remove(modelInfo.model.uuid);
            // Confirm it was deleted
            GenericSoundModel returnedModel = soundTriggerService.getSoundModel(new ParcelUuid(modelInfo.model.uuid));
            assertEquals(null, returnedModel);
            modelInfo.status = STATUS_UNLOADED;
        }
    }
}
Also used : ParcelUuid(android.os.ParcelUuid) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) GenericSoundModel(android.hardware.soundtrigger.SoundTrigger.GenericSoundModel) Random(java.util.Random) RecognitionConfig(android.hardware.soundtrigger.SoundTrigger.RecognitionConfig) UUID(java.util.UUID) Socket(java.net.Socket) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 2 with RecognitionConfig

use of android.hardware.soundtrigger.SoundTrigger.RecognitionConfig in project platform_frameworks_base by android.

the class GenericSoundModelTest method testStartStopGenericSoundModel.

@LargeTest
public void testStartStopGenericSoundModel() throws Exception {
    GenericSoundModel model = new_sound_model();
    boolean captureTriggerAudio = true;
    boolean allowMultipleTriggers = true;
    RecognitionConfig config = new RecognitionConfig(captureTriggerAudio, allowMultipleTriggers, null, null);
    TestRecognitionStatusCallback spyCallback = spy(new TestRecognitionStatusCallback());
    // Update and start sound model recognition
    soundTriggerService.updateSoundModel(model);
    loadedModelUuids.add(model.uuid);
    int r = soundTriggerService.startRecognition(new ParcelUuid(model.uuid), spyCallback, config);
    assertEquals("Could Not Start Recognition with code: " + r, android.hardware.soundtrigger.SoundTrigger.STATUS_OK, r);
    // Stop recognition
    r = soundTriggerService.stopRecognition(new ParcelUuid(model.uuid), spyCallback);
    assertEquals("Could Not Stop Recognition with code: " + r, android.hardware.soundtrigger.SoundTrigger.STATUS_OK, r);
}
Also used : ParcelUuid(android.os.ParcelUuid) GenericSoundModel(android.hardware.soundtrigger.SoundTrigger.GenericSoundModel) RecognitionConfig(android.hardware.soundtrigger.SoundTrigger.RecognitionConfig) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 3 with RecognitionConfig

use of android.hardware.soundtrigger.SoundTrigger.RecognitionConfig in project platform_frameworks_base by android.

the class SoundTriggerHelper method onKeyphraseRecognitionSuccessLocked.

private void onKeyphraseRecognitionSuccessLocked(KeyphraseRecognitionEvent event) {
    Slog.i(TAG, "Recognition success");
    MetricsLogger.count(mContext, "sth_keyphrase_recognition_event", 1);
    int keyphraseId = getKeyphraseIdFromEvent(event);
    ModelData modelData = getKeyphraseModelDataLocked(keyphraseId);
    if (modelData == null || !modelData.isKeyphraseModel()) {
        Slog.e(TAG, "Keyphase model data does not exist for ID:" + keyphraseId);
        return;
    }
    if (modelData.getCallback() == null) {
        Slog.w(TAG, "Received onRecognition event without callback for keyphrase model.");
        return;
    }
    try {
        modelData.getCallback().onKeyphraseDetected((KeyphraseRecognitionEvent) event);
    } catch (RemoteException e) {
        Slog.w(TAG, "RemoteException in onKeyphraseDetected", e);
    }
    modelData.setStopped();
    RecognitionConfig config = modelData.getRecognitionConfig();
    if (config != null) {
        // Whether we should continue by starting this again.
        modelData.setRequested(config.allowMultipleTriggers);
    }
    // TODO: Remove this block if the lower layer supports multiple triggers.
    if (modelData.isRequested()) {
        updateRecognitionLocked(modelData, isRecognitionAllowed(), true);
    }
}
Also used : RecognitionConfig(android.hardware.soundtrigger.SoundTrigger.RecognitionConfig) RemoteException(android.os.RemoteException)

Example 4 with RecognitionConfig

use of android.hardware.soundtrigger.SoundTrigger.RecognitionConfig in project android_frameworks_base by DirtyUnicorns.

the class SoundTriggerHelper method startRecognitionLocked.

// A single routine that implements the start recognition logic for both generic and keyphrase
// models.
private int startRecognitionLocked(ModelData modelData, boolean notify) {
    IRecognitionStatusCallback callback = modelData.getCallback();
    int handle = modelData.getHandle();
    RecognitionConfig config = modelData.getRecognitionConfig();
    if (callback == null || handle == INVALID_VALUE || config == null) {
        // Nothing to do here.
        Slog.w(TAG, "startRecognition: Bad data passed in.");
        MetricsLogger.count(mContext, "sth_start_recognition_error", 1);
        return STATUS_ERROR;
    }
    if (!isRecognitionAllowed()) {
        // Nothing to do here.
        Slog.w(TAG, "startRecognition requested but not allowed.");
        MetricsLogger.count(mContext, "sth_start_recognition_not_allowed", 1);
        return STATUS_OK;
    }
    int status = mModule.startRecognition(handle, config);
    if (status != SoundTrigger.STATUS_OK) {
        Slog.w(TAG, "startRecognition failed with " + status);
        MetricsLogger.count(mContext, "sth_start_recognition_error", 1);
        // Notify of error if needed.
        if (notify) {
            try {
                callback.onError(status);
            } catch (RemoteException e) {
                Slog.w(TAG, "RemoteException in onError", e);
            }
        }
    } else {
        Slog.i(TAG, "startRecognition successful.");
        MetricsLogger.count(mContext, "sth_start_recognition_success", 1);
        modelData.setStarted();
        // Notify of resume if needed.
        if (notify) {
            try {
                callback.onRecognitionResumed();
            } catch (RemoteException e) {
                Slog.w(TAG, "RemoteException in onRecognitionResumed", e);
            }
        }
    }
    if (DBG) {
        Slog.d(TAG, "Model being started :" + modelData.toString());
    }
    return status;
}
Also used : RecognitionConfig(android.hardware.soundtrigger.SoundTrigger.RecognitionConfig) RemoteException(android.os.RemoteException) IRecognitionStatusCallback(android.hardware.soundtrigger.IRecognitionStatusCallback)

Example 5 with RecognitionConfig

use of android.hardware.soundtrigger.SoundTrigger.RecognitionConfig in project android_frameworks_base by DirtyUnicorns.

the class AlwaysOnHotwordDetector method startRecognitionLocked.

private int startRecognitionLocked(int recognitionFlags) {
    KeyphraseRecognitionExtra[] recognitionExtra = new KeyphraseRecognitionExtra[1];
    // TODO: Do we need to do something about the confidence level here?
    recognitionExtra[0] = new KeyphraseRecognitionExtra(mKeyphraseMetadata.id, mKeyphraseMetadata.recognitionModeFlags, 0, new ConfidenceLevel[0]);
    boolean captureTriggerAudio = (recognitionFlags & RECOGNITION_FLAG_CAPTURE_TRIGGER_AUDIO) != 0;
    boolean allowMultipleTriggers = (recognitionFlags & RECOGNITION_FLAG_ALLOW_MULTIPLE_TRIGGERS) != 0;
    int code = STATUS_ERROR;
    try {
        code = mModelManagementService.startRecognition(mVoiceInteractionService, mKeyphraseMetadata.id, mLocale.toLanguageTag(), mInternalCallback, new RecognitionConfig(captureTriggerAudio, allowMultipleTriggers, recognitionExtra, null));
    } catch (RemoteException e) {
        Slog.w(TAG, "RemoteException in startRecognition!", e);
    }
    if (code != STATUS_OK) {
        Slog.w(TAG, "startRecognition() failed with error code " + code);
    }
    return code;
}
Also used : KeyphraseRecognitionExtra(android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionExtra) ConfidenceLevel(android.hardware.soundtrigger.SoundTrigger.ConfidenceLevel) RecognitionConfig(android.hardware.soundtrigger.SoundTrigger.RecognitionConfig) RemoteException(android.os.RemoteException)

Aggregations

RecognitionConfig (android.hardware.soundtrigger.SoundTrigger.RecognitionConfig)29 RemoteException (android.os.RemoteException)17 GenericSoundModel (android.hardware.soundtrigger.SoundTrigger.GenericSoundModel)12 ParcelUuid (android.os.ParcelUuid)12 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 IRecognitionStatusCallback (android.hardware.soundtrigger.IRecognitionStatusCallback)8 DataOutputStream (java.io.DataOutputStream)8 Socket (java.net.Socket)8 ConfidenceLevel (android.hardware.soundtrigger.SoundTrigger.ConfidenceLevel)5 KeyphraseRecognitionExtra (android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionExtra)5 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 UUID (java.util.UUID)4