use of android.hardware.soundtrigger.IRecognitionStatusCallback in project android_frameworks_base by crdroidandroid.
the class SoundTriggerHelper method startRecognition.
/**
* Starts recognition for the given sound model. A single routine for both keyphrase and
* generic sound models.
*
* @param soundModel The sound model to use for recognition.
* @param modelData Instance of {@link #ModelData} for the given model.
* @param callback Callback for the recognition events related to the given keyphrase.
* @param recognitionConfig Instance of {@link RecognitionConfig} containing the parameters
* @param keyphraseId Keyphrase ID for keyphrase models only. Pass in INVALID_VALUE for other
* models.
* for the recognition.
* @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}.
*/
int startRecognition(SoundModel soundModel, ModelData modelData, IRecognitionStatusCallback callback, RecognitionConfig recognitionConfig, int keyphraseId) {
synchronized (mLock) {
if (mModuleProperties == null) {
Slog.w(TAG, "Attempting startRecognition without the capability");
return STATUS_ERROR;
}
if (mModule == null) {
mModule = SoundTrigger.attachModule(mModuleProperties.id, this, null);
if (mModule == null) {
Slog.w(TAG, "startRecognition cannot attach to sound trigger module");
return STATUS_ERROR;
}
}
// Initialize power save, call active state monitoring logic.
if (!mRecognitionRunning) {
initializeTelephonyAndPowerStateListeners();
}
// startKeyphrase() routine.
if (modelData.getSoundModel() != null) {
// Stop the model after checking that it is started.
boolean stopModel = false;
boolean unloadModel = false;
if (modelData.getSoundModel().equals(soundModel) && modelData.isModelStarted()) {
// The model has not changed, but the previous model is "started".
// Stop the previously running model.
stopModel = true;
// No need to unload if the model hasn't changed.
unloadModel = false;
} else if (!modelData.getSoundModel().equals(soundModel)) {
// We have a different model for this UUID. Stop and unload if needed. This
// helps maintain the singleton restriction for keyphrase sound models.
stopModel = modelData.isModelStarted();
unloadModel = modelData.isModelLoaded();
}
if (stopModel || unloadModel) {
int status = tryStopAndUnloadLocked(modelData, stopModel, unloadModel);
if (status != STATUS_OK) {
Slog.w(TAG, "Unable to stop or unload previous model: " + modelData.toString());
return status;
}
}
}
IRecognitionStatusCallback oldCallback = modelData.getCallback();
if (oldCallback != null && oldCallback.asBinder() != callback.asBinder()) {
Slog.w(TAG, "Canceling previous recognition for model id: " + modelData.getModelId());
try {
oldCallback.onError(STATUS_ERROR);
} catch (RemoteException e) {
Slog.w(TAG, "RemoteException in onDetectionStopped", e);
}
modelData.clearCallback();
}
// Load the model if it is not loaded.
if (!modelData.isModelLoaded()) {
// Load the model
int[] handle = new int[] { INVALID_VALUE };
int status = mModule.loadSoundModel(soundModel, handle);
if (status != SoundTrigger.STATUS_OK) {
Slog.w(TAG, "loadSoundModel call failed with " + status);
return status;
}
if (handle[0] == INVALID_VALUE) {
Slog.w(TAG, "loadSoundModel call returned invalid sound model handle");
return STATUS_ERROR;
}
modelData.setHandle(handle[0]);
modelData.setLoaded();
Slog.d(TAG, "Sound model loaded with handle:" + handle[0]);
}
modelData.setCallback(callback);
modelData.setRequested(true);
modelData.setRecognitionConfig(recognitionConfig);
modelData.setSoundModel(soundModel);
return startRecognitionLocked(modelData, false);
}
}
use of android.hardware.soundtrigger.IRecognitionStatusCallback in project platform_frameworks_base by android.
the class SoundTriggerHelper method onGenericRecognitionSuccessLocked.
private void onGenericRecognitionSuccessLocked(GenericRecognitionEvent event) {
MetricsLogger.count(mContext, "sth_generic_recognition_event", 1);
if (event.status != SoundTrigger.RECOGNITION_STATUS_SUCCESS) {
return;
}
ModelData model = getModelDataForLocked(event.soundModelHandle);
if (model == null || !model.isGenericModel()) {
Slog.w(TAG, "Generic recognition event: Model does not exist for handle: " + event.soundModelHandle);
return;
}
IRecognitionStatusCallback callback = model.getCallback();
if (callback == null) {
Slog.w(TAG, "Generic recognition event: Null callback for model handle: " + event.soundModelHandle);
return;
}
try {
callback.onGenericSoundTriggerDetected((GenericRecognitionEvent) event);
} catch (RemoteException e) {
Slog.w(TAG, "RemoteException in onGenericSoundTriggerDetected", e);
}
model.setStopped();
RecognitionConfig config = model.getRecognitionConfig();
if (config == null) {
Slog.w(TAG, "Generic recognition event: Null RecognitionConfig for model handle: " + event.soundModelHandle);
return;
}
model.setRequested(config.allowMultipleTriggers);
// TODO: Remove this block if the lower layer supports multiple triggers.
if (model.isRequested()) {
updateRecognitionLocked(model, isRecognitionAllowed(), /* isAllowed */
true);
}
}
use of android.hardware.soundtrigger.IRecognitionStatusCallback in project platform_frameworks_base by android.
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;
}
use of android.hardware.soundtrigger.IRecognitionStatusCallback in project platform_frameworks_base by android.
the class SoundTriggerHelper method stopRecognition.
/**
* Stops recognition for the given ModelData instance.
*
* @param modelData Instance of {@link #ModelData} sound model.
* @param callback The callback for the recognition events related to the given keyphrase.
* @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}.
*/
private int stopRecognition(ModelData modelData, IRecognitionStatusCallback callback) {
synchronized (mLock) {
if (callback == null) {
return STATUS_ERROR;
}
if (mModuleProperties == null || mModule == null) {
Slog.w(TAG, "Attempting stopRecognition without the capability");
return STATUS_ERROR;
}
IRecognitionStatusCallback currentCallback = modelData.getCallback();
if (modelData == null || currentCallback == null || (!modelData.isRequested() && !modelData.isModelStarted())) {
// startGenericRecognition hasn't been called or it failed.
Slog.w(TAG, "Attempting stopRecognition without a successful startRecognition");
return STATUS_ERROR;
}
if (currentCallback.asBinder() != callback.asBinder()) {
// We don't allow a different listener to stop the recognition than the one
// that started it.
Slog.w(TAG, "Attempting stopRecognition for another recognition");
return STATUS_ERROR;
}
// Request stop recognition via the update() method.
modelData.setRequested(false);
int status = updateRecognitionLocked(modelData, isRecognitionAllowed(), false);
if (status != SoundTrigger.STATUS_OK) {
return status;
}
// We leave the sound model loaded but not started, this helps us when we start back.
// Also clear the internal state once the recognition has been stopped.
modelData.setLoaded();
modelData.clearCallback();
modelData.setRecognitionConfig(null);
if (!computeRecognitionRunningLocked()) {
internalClearGlobalStateLocked();
}
return status;
}
}
use of android.hardware.soundtrigger.IRecognitionStatusCallback in project android_frameworks_base by DirtyUnicorns.
the class SoundTriggerHelper method onGenericRecognitionSuccessLocked.
private void onGenericRecognitionSuccessLocked(GenericRecognitionEvent event) {
MetricsLogger.count(mContext, "sth_generic_recognition_event", 1);
if (event.status != SoundTrigger.RECOGNITION_STATUS_SUCCESS) {
return;
}
ModelData model = getModelDataForLocked(event.soundModelHandle);
if (model == null || !model.isGenericModel()) {
Slog.w(TAG, "Generic recognition event: Model does not exist for handle: " + event.soundModelHandle);
return;
}
IRecognitionStatusCallback callback = model.getCallback();
if (callback == null) {
Slog.w(TAG, "Generic recognition event: Null callback for model handle: " + event.soundModelHandle);
return;
}
try {
callback.onGenericSoundTriggerDetected((GenericRecognitionEvent) event);
} catch (RemoteException e) {
Slog.w(TAG, "RemoteException in onGenericSoundTriggerDetected", e);
}
model.setStopped();
RecognitionConfig config = model.getRecognitionConfig();
if (config == null) {
Slog.w(TAG, "Generic recognition event: Null RecognitionConfig for model handle: " + event.soundModelHandle);
return;
}
model.setRequested(config.allowMultipleTriggers);
// TODO: Remove this block if the lower layer supports multiple triggers.
if (model.isRequested()) {
updateRecognitionLocked(model, isRecognitionAllowed(), /* isAllowed */
true);
}
}
Aggregations