Merge changes from topic "getmodelstate-redo"

* changes:
  Rolling forward with indentation fix.
  Revert "Revert "Adding getModelState API to sound trigger""
This commit is contained in:
Michael Dooley
2018-10-24 06:04:42 +00:00
committed by Android (Google) Code Review
6 changed files with 157 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ import android.annotation.UnsupportedAppUsage;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.lang.ref.WeakReference;
/**
@@ -131,6 +132,14 @@ public class SoundTriggerModule {
@UnsupportedAppUsage
public native int stopRecognition(int soundModelHandle);
/**
* Get the current state of a {@link SoundTrigger.SoundModel}
* @param soundModelHandle The sound model handle indicating which model's state to return
* @return - {@link SoundTrigger#RecognitionEvent} in case of success
* - null in case of an error or if not supported
*/
public native SoundTrigger.RecognitionEvent getModelState(int soundModelHandle);
private class NativeEventHandlerDelegate {
private final Handler mHandler;
@@ -207,4 +216,3 @@ public class SoundTriggerModule {
}
}
}

View File

@@ -52,4 +52,6 @@ interface ISoundTriggerService {
/** For both ...Intent and ...Service based usage */
boolean isRecognitionActive(in ParcelUuid parcelUuid);
SoundTrigger.RecognitionEvent getModelState(in ParcelUuid parcelUuid);
}

View File

@@ -788,6 +788,63 @@ android_hardware_SoundTrigger_stopRecognition(JNIEnv *env, jobject thiz,
return status;
}
static jobject
android_hardware_SoundTrigger_getModelState(JNIEnv *env, jobject thiz,
jint jHandle)
{
ALOGV("getModelState");
sp<SoundTrigger> module = getSoundTrigger(env, thiz);
if (module == NULL) {
return NULL;
}
sp<IMemory> memory;
jint status = module->getModelState(jHandle, memory);
if (status != 0 || memory == NULL) {
ALOGW("getModelState, failed to get model state, status: %d", status);
return NULL;
}
struct sound_trigger_recognition_event* event =
(struct sound_trigger_recognition_event *)memory->pointer();
if (event == NULL) {
return NULL;
}
if (event->type != SOUND_MODEL_TYPE_GENERIC) {
ALOGW("getModelState, unsupported model type: %d", event->type);
return NULL;
}
jbyteArray jData = NULL;
if (event->data_size) {
jData = env->NewByteArray(event->data_size);
jbyte *nData = env->GetByteArrayElements(jData, NULL);
memcpy(nData, (char *)event + event->data_offset, event->data_size);
env->ReleaseByteArrayElements(jData, nData, 0);
}
jobject jAudioFormat = NULL;
if (event->trigger_in_data || event->capture_available) {
jAudioFormat = env->NewObject(gAudioFormatClass,
gAudioFormatCstor,
audioFormatFromNative(event->audio_config.format),
event->audio_config.sample_rate,
inChannelMaskFromNative(event->audio_config.channel_mask));
}
jobject jEvent = NULL;
jEvent = env->NewObject(gGenericRecognitionEventClass, gGenericRecognitionEventCstor,
event->status, event->model, event->capture_available,
event->capture_session, event->capture_delay_ms,
event->capture_preamble_ms, event->trigger_in_data,
jAudioFormat, jData);
if (jAudioFormat != NULL) {
env->DeleteLocalRef(jAudioFormat);
}
if (jData != NULL) {
env->DeleteLocalRef(jData);
}
return jEvent;
}
static const JNINativeMethod gMethods[] = {
{"listModules",
"(Ljava/util/ArrayList;)I",
@@ -817,6 +874,9 @@ static const JNINativeMethod gModuleMethods[] = {
{"stopRecognition",
"(I)I",
(void *)android_hardware_SoundTrigger_stopRecognition},
{"getModelState",
"(I)Landroid/hardware/soundtrigger/SoundTrigger$RecognitionEvent;",
(void *)android_hardware_SoundTrigger_getModelState},
};
int register_android_hardware_SoundTrigger(JNIEnv *env)

View File

@@ -365,4 +365,22 @@ public final class SoundTriggerManager {
return Integer.MAX_VALUE;
}
}
/**
* Synchronously get state of the indicated model. The model state is returned as
* a recognition event, or null if the model is not loaded, or if this method
* is not supported.
* @hide
*/
@RequiresPermission(android.Manifest.permission.MANAGE_SOUND_TRIGGER)
public SoundTrigger.RecognitionEvent getModelState(UUID soundModelId) {
if (soundModelId == null) {
return null;
}
try {
return mSoundTriggerService.getModelState(new ParcelUuid(soundModelId));
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}

View File

@@ -566,6 +566,40 @@ public class SoundTriggerHelper implements SoundTrigger.StatusListener {
}
}
SoundTrigger.RecognitionEvent getGenericModelState(UUID modelId) {
synchronized (mLock) {
MetricsLogger.count(mContext, "sth_get_generic_model_state", 1);
if (modelId == null || mModule == null) {
return null;
}
ModelData modelData = mModelDataMap.get(modelId);
if (modelData == null || !modelData.isGenericModel()) {
Slog.w(TAG, "GetGenericModelState error: Invalid generic model id:" +
modelId);
return null;
}
if (!modelData.isModelLoaded()) {
Slog.i(TAG, "GetGenericModelState: Given generic model is not loaded:" + modelId);
return null;
}
if (!modelData.isModelStarted()) {
Slog.i(TAG, "GetGenericModelState: Given generic model is not started:" + modelId);
return null;
}
SoundTrigger.RecognitionEvent ret = mModule.getModelState(modelData.getHandle());
if (ret == null) {
Slog.w(TAG, "GetGenericModelState() call failed");
}
return ret;
}
}
SoundTrigger.RecognitionEvent getKeyphraseModelState(UUID modelId) {
Slog.w(TAG, "GetKeyphraseModelState error: Not implemented");
return null;
}
//---- SoundTrigger.StatusListener methods
@Override
public void onRecognition(RecognitionEvent event) {

View File

@@ -434,6 +434,40 @@ public class SoundTriggerService extends SystemService {
}
return mSoundTriggerHelper.isRecognitionRequested(parcelUuid.getUuid());
}
@Override
public SoundTrigger.RecognitionEvent getModelState(ParcelUuid soundModelId) {
enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
if (!isInitialized()) return null;
if (DEBUG) {
Slog.i(TAG, "getModelState(): id = " + soundModelId);
}
synchronized (mLock) {
SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
if (soundModel == null) {
Slog.e(TAG, soundModelId + " is not loaded");
return null;
}
SoundTrigger.RecognitionEvent ret = null;
switch (soundModel.type) {
case SoundModel.TYPE_KEYPHRASE:
ret = mSoundTriggerHelper.getKeyphraseModelState(soundModel.uuid);
break;
case SoundModel.TYPE_GENERIC_SOUND:
ret = mSoundTriggerHelper.getGenericModelState(soundModel.uuid);
break;
default:
Slog.e(TAG, "Unknown model type");
break;
}
if (ret == null) {
Slog.e(TAG, "Failed to get model state");
}
return ret;
}
}
}
/**