Merge changes from topic "invert-capture-rvc" into rvc-dev

* changes:
  Register for capture state notifications via JNI
  Remove obsolete permission
  Change external capture state notification mechanism
This commit is contained in:
Ytai Ben-tsvi
2020-04-01 22:57:39 +00:00
committed by Android (Google) Code Review
14 changed files with 251 additions and 61 deletions

View File

@@ -4712,12 +4712,6 @@
<permission android:name="android.permission.MANAGE_SOUND_TRIGGER"
android:protectionLevel="signature|privileged" />
<!-- Allows preempting sound trigger recognitions for the sake of capturing audio on
implementations which do not support running both concurrently.
@hide -->
<permission android:name="android.permission.PREEMPT_SOUND_TRIGGER"
android:protectionLevel="signature|privileged" />
<!-- Must be required by system/priv apps implementing sound trigger detection services
@hide
@SystemApi -->

View File

@@ -162,7 +162,6 @@
<assign-permission name="android.permission.UPDATE_DEVICE_STATS" uid="audioserver" />
<assign-permission name="android.permission.UPDATE_APP_OPS_STATS" uid="audioserver" />
<assign-permission name="android.permission.PACKAGE_USAGE_STATS" uid="audioserver" />
<assign-permission name="android.permission.PREEMPT_SOUND_TRIGGER" uid="audioserver" />
<assign-permission name="android.permission.MODIFY_AUDIO_SETTINGS" uid="cameraserver" />
<assign-permission name="android.permission.ACCESS_SURFACE_FLINGER" uid="cameraserver" />

View File

@@ -39,10 +39,4 @@ interface ISoundTriggerMiddlewareService {
* one of the handles from the returned list.
*/
ISoundTriggerModule attach(int handle, ISoundTriggerCallback callback);
/**
* Notify the service that external input capture is taking place. This may cause some of the
* active recognitions to be aborted.
*/
void setExternalCaptureState(boolean active);
}

View File

@@ -126,6 +126,7 @@ java_library_static {
"android.hardware.rebootescrow-java",
"android.hardware.soundtrigger-V2.3-java",
"android.hidl.manager-V1.2-java",
"capture_state_listener-aidl-java",
"dnsresolver_aidl_interface-V2-java",
"netd_event_listener_interface-java",
"overlayable_policy_aidl-java",

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.soundtrigger_middleware;
import android.media.ICaptureStateListener;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import java.util.concurrent.Semaphore;
import java.util.function.Consumer;
/**
* This is a never-give-up listener for sound trigger external capture state notifications, as
* published by the audio policy service.
*
* This class will constantly try to connect to the service over a background thread and tolerate
* its death. The client will be notified by a single provided function that is called in a
* synchronized manner.
* For simplicity, there is currently no way to stop the tracker. This is possible to add if the
* need ever arises.
*/
class ExternalCaptureStateTracker {
private static final String TAG = "CaptureStateTracker";
/** Our client's listener. */
private final Consumer<Boolean> mListener;
/** This semaphore will get a permit every time we need to reconnect. */
private final Semaphore mNeedToConnect = new Semaphore(1);
/**
* Constructor. Will start a background thread to do the work.
*
* @param listener A client provided listener that will be called on state
* changes. May be
* called multiple consecutive times with the same value. Never
* called
* concurrently.
*/
ExternalCaptureStateTracker(Consumer<Boolean> listener) {
mListener = listener;
new Thread(this::run).start();
}
/**
* Routine for the background thread. Keeps trying to reconnect.
*/
private void run() {
while (true) {
mNeedToConnect.acquireUninterruptibly();
connect();
}
}
/**
* Connect to the service, install listener and death notifier.
*/
private native void connect();
/**
* Called by native code to invoke the client listener.
*
* @param active true when external capture is active.
*/
private void setCaptureState(boolean active) {
mListener.accept(active);
}
/**
* Called by native code when the remote service died.
*/
private void binderDied() {
Log.w(TAG, "Audio policy service died");
mNeedToConnect.release();
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.soundtrigger_middleware;
import android.media.ICaptureStateListener;
import android.media.soundtrigger_middleware.ISoundTriggerMiddlewareService;
/**
* This interface unifies ISoundTriggerMiddlewareService with ICaptureStateListener.
*/
public interface ISoundTriggerMiddlewareInternal extends ISoundTriggerMiddlewareService,
ICaptureStateListener {
}

View File

@@ -50,7 +50,7 @@ import java.util.List;
*
* @hide
*/
public class SoundTriggerMiddlewareImpl implements ISoundTriggerMiddlewareService {
public class SoundTriggerMiddlewareImpl implements ISoundTriggerMiddlewareInternal {
static private final String TAG = "SoundTriggerMiddlewareImpl";
private final SoundTriggerModule[] mModules;
@@ -124,7 +124,7 @@ public class SoundTriggerMiddlewareImpl implements ISoundTriggerMiddlewareServic
}
@Override
public void setExternalCaptureState(boolean active) {
public void setCaptureState(boolean active) {
for (SoundTriggerModule module : mModules) {
module.setExternalCaptureState(active);
}

View File

@@ -62,11 +62,11 @@ import java.util.LinkedList;
* String, Object, Object[])}, {@link #logVoidReturnWithObject(Object, String, Object[])} and {@link
* #logExceptionWithObject(Object, String, Exception, Object[])}.
*/
public class SoundTriggerMiddlewareLogging implements ISoundTriggerMiddlewareService, Dumpable {
public class SoundTriggerMiddlewareLogging implements ISoundTriggerMiddlewareInternal, Dumpable {
private static final String TAG = "SoundTriggerMiddlewareLogging";
private final @NonNull ISoundTriggerMiddlewareService mDelegate;
private final @NonNull ISoundTriggerMiddlewareInternal mDelegate;
public SoundTriggerMiddlewareLogging(@NonNull ISoundTriggerMiddlewareService delegate) {
public SoundTriggerMiddlewareLogging(@NonNull ISoundTriggerMiddlewareInternal delegate) {
mDelegate = delegate;
}
@@ -96,12 +96,12 @@ public class SoundTriggerMiddlewareLogging implements ISoundTriggerMiddlewareSer
}
@Override
public void setExternalCaptureState(boolean active) throws RemoteException {
public void setCaptureState(boolean active) throws RemoteException {
try {
mDelegate.setExternalCaptureState(active);
logVoidReturn("setExternalCaptureState", active);
mDelegate.setCaptureState(active);
logVoidReturn("setCaptureState", active);
} catch (Exception e) {
logException("setExternalCaptureState", e, active);
logException("setCaptureState", e, active);
throw e;
}
}

View File

@@ -63,14 +63,21 @@ public class SoundTriggerMiddlewareService extends ISoundTriggerMiddlewareServic
static private final String TAG = "SoundTriggerMiddlewareService";
@NonNull
private final ISoundTriggerMiddlewareService mDelegate;
private final ISoundTriggerMiddlewareInternal mDelegate;
/**
* Constructor for internal use only. Could be exposed for testing purposes in the future.
* Users should access this class via {@link Lifecycle}.
*/
private SoundTriggerMiddlewareService(@NonNull ISoundTriggerMiddlewareService delegate) {
private SoundTriggerMiddlewareService(@NonNull ISoundTriggerMiddlewareInternal delegate) {
mDelegate = Objects.requireNonNull(delegate);
new ExternalCaptureStateTracker(active -> {
try {
mDelegate.setCaptureState(active);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
});
}
@Override
@@ -86,11 +93,6 @@ public class SoundTriggerMiddlewareService extends ISoundTriggerMiddlewareServic
return new ModuleService(mDelegate.attach(handle, callback));
}
@Override
public void setExternalCaptureState(boolean active) throws RemoteException {
mDelegate.setExternalCaptureState(active);
}
@Override protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
if (mDelegate instanceof Dumpable) {
((Dumpable) mDelegate).dump(fout);

View File

@@ -105,7 +105,7 @@ import java.util.Set;
*
* {@hide}
*/
public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddlewareService, Dumpable {
public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddlewareInternal, Dumpable {
private static final String TAG = "SoundTriggerMiddlewareValidation";
private enum ModuleState {
@@ -114,12 +114,12 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
DEAD
};
private final @NonNull ISoundTriggerMiddlewareService mDelegate;
private final @NonNull ISoundTriggerMiddlewareInternal mDelegate;
private final @NonNull Context mContext;
private Map<Integer, Set<ModuleService>> mModules;
public SoundTriggerMiddlewareValidation(
@NonNull ISoundTriggerMiddlewareService delegate, @NonNull Context context) {
@NonNull ISoundTriggerMiddlewareInternal delegate, @NonNull Context context) {
mDelegate = delegate;
mContext = context;
}
@@ -213,21 +213,15 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
}
@Override
public void setExternalCaptureState(boolean active) {
// Permission check.
checkPreemptPermissions();
// Input validation (always valid).
// State validation (always valid).
public void setCaptureState(boolean active) {
// This is an internal call. No permissions needed.
//
// Normally, we would acquire a lock here. However, we do not access any state here so it
// is safe to not lock. This call is typically done from a different context than all the
// other calls and may result in a deadlock if we lock here (between the audio server and
// the system server).
// From here on, every exception isn't client's fault.
try {
mDelegate.setExternalCaptureState(active);
mDelegate.setCaptureState(active);
} catch (Exception e) {
throw handleException(e);
}
@@ -249,16 +243,6 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
enforcePermission(Manifest.permission.CAPTURE_AUDIO_HOTWORD);
}
/**
* Throws a {@link SecurityException} if caller permanently doesn't have the given permission,
* or a {@link ServiceSpecificException} with a {@link Status#TEMPORARY_PERMISSION_DENIED} if
* caller temporarily doesn't have the right permissions to preempt active sound trigger
* sessions.
*/
void checkPreemptPermissions() {
enforcePermission(Manifest.permission.PREEMPT_SOUND_TRIGGER);
}
/**
* Throws a {@link SecurityException} if caller permanently doesn't have the given permission,
* or a {@link ServiceSpecificException} with a {@link Status#TEMPORARY_PERMISSION_DENIED} if
@@ -806,4 +790,4 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
}
}
}
}
}

View File

@@ -40,6 +40,7 @@ cc_library_static {
"com_android_server_security_VerityUtils.cpp",
"com_android_server_SerialService.cpp",
"com_android_server_soundtrigger_middleware_AudioSessionProviderImpl.cpp",
"com_android_server_soundtrigger_middleware_ExternalCaptureStateTracker.cpp",
"com_android_server_stats_pull_StatsPullAtomService.cpp",
"com_android_server_storage_AppFuseBridge.cpp",
"com_android_server_SystemServer.cpp",

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sstream>
#define LOG_TAG "ExternalCaptureStateTracker"
#include "core_jni_helpers.h"
#include <log/log.h>
#include <media/AudioSystem.h>
namespace android {
namespace {
#define PACKAGE "com/android/server/soundtrigger_middleware"
#define CLASSNAME PACKAGE "/ExternalCaptureStateTracker"
jclass gExternalCaptureStateTrackerClassId;
jmethodID gSetCaptureStateMethodId;
jmethodID gBinderDiedMethodId;
void PopulateIds(JNIEnv* env) {
gExternalCaptureStateTrackerClassId =
(jclass) env->NewGlobalRef(FindClassOrDie(env, CLASSNAME));
gSetCaptureStateMethodId = GetMethodIDOrDie(env,
gExternalCaptureStateTrackerClassId,
"setCaptureState",
"(Z)V");
gBinderDiedMethodId = GetMethodIDOrDie(env,
gExternalCaptureStateTrackerClassId,
"binderDied",
"()V");
}
class Listener : public AudioSystem::CaptureStateListener {
public:
Listener(JNIEnv* env, jobject obj) : mObj(env->NewGlobalRef(obj)) {}
~Listener() {
JNIEnv* env = AndroidRuntime::getJNIEnv();
env->DeleteGlobalRef(mObj);
}
void onStateChanged(bool active) override {
JNIEnv* env = AndroidRuntime::getJNIEnv();
env->CallVoidMethod(mObj, gSetCaptureStateMethodId, active);
}
void onServiceDied() override {
JNIEnv* env = AndroidRuntime::getJNIEnv();
env->CallVoidMethod(mObj, gBinderDiedMethodId);
}
private:
jobject mObj;
};
void connect(JNIEnv* env, jobject obj) {
sp<AudioSystem::CaptureStateListener> listener(new Listener(env, obj));
status_t status =
AudioSystem::registerSoundTriggerCaptureStateListener(listener);
LOG_ALWAYS_FATAL_IF(status != NO_ERROR);
}
const JNINativeMethod gMethods[] = {
{"connect", "()V", reinterpret_cast<void*>(connect)},
};
} // namespace
int register_com_android_server_soundtrigger_middleware_ExternalCaptureStateTracker(
JNIEnv* env) {
PopulateIds(env);
return RegisterMethodsOrDie(env,
CLASSNAME,
gMethods,
NELEM(gMethods));
}
} // namespace android

View File

@@ -58,6 +58,8 @@ int register_android_server_am_CachedAppOptimizer(JNIEnv* env);
int register_android_server_am_LowMemDetector(JNIEnv* env);
int register_com_android_server_soundtrigger_middleware_AudioSessionProviderImpl(
JNIEnv* env);
int register_com_android_server_soundtrigger_middleware_ExternalCaptureStateTracker(
JNIEnv* env);
int register_android_server_com_android_server_pm_PackageManagerShellCommandDataLoader(JNIEnv* env);
int register_android_server_stats_pull_StatsPullAtomService(JNIEnv* env);
int register_android_server_AdbDebuggingManager(JNIEnv* env);
@@ -112,6 +114,8 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
register_android_server_am_LowMemDetector(env);
register_com_android_server_soundtrigger_middleware_AudioSessionProviderImpl(
env);
register_com_android_server_soundtrigger_middleware_ExternalCaptureStateTracker(
env);
register_android_server_com_android_server_pm_PackageManagerShellCommandDataLoader(env);
register_android_server_stats_pull_StatsPullAtomService(env);
register_android_server_AdbDebuggingManager(env);

View File

@@ -1106,7 +1106,7 @@ public class SoundTriggerMiddlewareImplTest {
public void testAbortRecognition() throws Exception {
// Make sure the HAL doesn't support concurrent capture.
initService(false);
mService.setExternalCaptureState(false);
mService.setCaptureState(false);
ISoundTriggerCallback callback = createCallbackMock();
ISoundTriggerModule module = mService.attach(0, callback);
@@ -1120,7 +1120,7 @@ public class SoundTriggerMiddlewareImplTest {
startRecognition(module, handle, hwHandle);
// Abort.
mService.setExternalCaptureState(true);
mService.setCaptureState(true);
ArgumentCaptor<RecognitionEvent> eventCaptor = ArgumentCaptor.forClass(
RecognitionEvent.class);
@@ -1142,7 +1142,7 @@ public class SoundTriggerMiddlewareImplTest {
verifyNotStartRecognition();
// Now enable it and make sure we are notified.
mService.setExternalCaptureState(false);
mService.setCaptureState(false);
verify(callback).onRecognitionAvailabilityChange(true);
// Unload the model.
@@ -1154,7 +1154,7 @@ public class SoundTriggerMiddlewareImplTest {
public void testAbortPhraseRecognition() throws Exception {
// Make sure the HAL doesn't support concurrent capture.
initService(false);
mService.setExternalCaptureState(false);
mService.setCaptureState(false);
ISoundTriggerCallback callback = createCallbackMock();
ISoundTriggerModule module = mService.attach(0, callback);
@@ -1168,7 +1168,7 @@ public class SoundTriggerMiddlewareImplTest {
startRecognition(module, handle, hwHandle);
// Abort.
mService.setExternalCaptureState(true);
mService.setCaptureState(true);
ArgumentCaptor<PhraseRecognitionEvent> eventCaptor = ArgumentCaptor.forClass(
PhraseRecognitionEvent.class);
@@ -1190,7 +1190,7 @@ public class SoundTriggerMiddlewareImplTest {
verifyNotStartRecognition();
// Now enable it and make sure we are notified.
mService.setExternalCaptureState(false);
mService.setCaptureState(false);
verify(callback).onRecognitionAvailabilityChange(true);
// Unload the model.
@@ -1216,7 +1216,7 @@ public class SoundTriggerMiddlewareImplTest {
startRecognition(module, handle, hwHandle);
// Signal concurrent capture. Shouldn't abort.
mService.setExternalCaptureState(true);
mService.setCaptureState(true);
verify(callback, never()).onRecognition(anyInt(), any());
verify(callback, never()).onRecognitionAvailabilityChange(anyBoolean());
@@ -1252,7 +1252,7 @@ public class SoundTriggerMiddlewareImplTest {
startRecognition(module, handle, hwHandle);
// Signal concurrent capture. Shouldn't abort.
mService.setExternalCaptureState(true);
mService.setCaptureState(true);
verify(callback, never()).onPhraseRecognition(anyInt(), any());
verify(callback, never()).onRecognitionAvailabilityChange(anyBoolean());