cmsdk: Broker out CMAudioService

Change-Id: Ia1205ad67d524ebf379085a6287993b8b82bc76f
Signed-off-by: Roman Birg <roman@cyngn.com>
This commit is contained in:
Roman Birg 2016-07-06 13:32:16 -07:00
parent 86cae92291
commit 705716fc60
8 changed files with 144 additions and 336 deletions

View File

@ -17,7 +17,6 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
src/org_cyanogenmod_platform_internal_CMAudioService.cpp \
src/org_cyanogenmod_platform_internal_PerformanceManagerService.cpp \
src/onload.cpp

View File

@ -21,7 +21,6 @@
namespace android {
int register_org_cyanogenmod_platform_internal_CMAudioService(JNIEnv* env);
int register_org_cyanogenmod_platform_internal_PerformanceManagerService(JNIEnv* env);
};
@ -39,7 +38,6 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
}
ALOG_ASSERT(env, "Could not retrieve the env!");
register_org_cyanogenmod_platform_internal_CMAudioService(env);
register_org_cyanogenmod_platform_internal_PerformanceManagerService(env);
return JNI_VERSION_1_4;

View File

@ -1,170 +0,0 @@
/*
**
** Copyright 2016, The CyanogenMod 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.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "CMAudioService-JNI"
#include <utils/Log.h>
#include <JNIHelp.h>
#include <jni.h>
#include "core_jni_helpers.h"
#include "android_media_AudioErrors.h"
#include <media/AudioSystem.h>
#include <media/AudioSession.h>
#include <system/audio.h>
#include <utils/threads.h>
// ----------------------------------------------------------------------------
namespace android {
static const char* const kClassPathName = "org/cyanogenmod/platform/internal/CMAudioService";
static jclass gArrayListClass;
static struct {
jmethodID add;
jmethodID toArray;
} gArrayListMethods;
static struct {
jmethodID postAudioSessionEventFromNative;
} gAudioSessionEventHandlerMethods;
static jclass gAudioSessionInfoClass;
static jmethodID gAudioSessionInfoCstor;
static jobject gThiz;
static Mutex gCallbackLock;
// ----------------------------------------------------------------------------
static void
org_cyanogenmod_platform_internal_CMAudioService_session_info_callback(int event,
sp<AudioSessionInfo>& info, bool added)
{
AutoMutex _l(gCallbackLock);
JNIEnv *env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
return;
}
jobject jSession = env->NewObject(gAudioSessionInfoClass, gAudioSessionInfoCstor,
info->mSessionId, info->mStream, info->mFlags, info->mChannelMask, info->mUid);
env->CallVoidMethod(gThiz,
gAudioSessionEventHandlerMethods.postAudioSessionEventFromNative,
event, jSession, added);
env->DeleteLocalRef(jSession);
}
static void
org_cyanogenmod_platform_internal_CMAudioService_registerAudioSessionCallback(
JNIEnv *env, jobject thiz, jboolean enabled)
{
if (gThiz == NULL) {
gThiz = env->NewGlobalRef(thiz);
}
AudioSystem::setAudioSessionCallback( enabled ?
org_cyanogenmod_platform_internal_CMAudioService_session_info_callback : NULL);
}
static jint
org_cyanogenmod_platform_internal_CMAudioService_listAudioSessions(JNIEnv *env, jobject thiz,
jint streams, jobject jSessions)
{
ALOGV("listAudioSessions");
if (jSessions == NULL) {
ALOGE("listAudioSessions NULL arraylist");
return (jint)AUDIO_JAVA_BAD_VALUE;
}
if (!env->IsInstanceOf(jSessions, gArrayListClass)) {
ALOGE("listAudioSessions not an arraylist");
return (jint)AUDIO_JAVA_BAD_VALUE;
}
status_t status;
Vector< sp<AudioSessionInfo>> sessions;
status = AudioSystem::listAudioSessions((audio_stream_type_t)streams, sessions);
if (status != NO_ERROR) {
ALOGE("AudioSystem::listAudioSessions error %d", status);
} else {
ALOGV("AudioSystem::listAudioSessions count=%zu", sessions.size());
}
jint jStatus = nativeToJavaStatus(status);
if (jStatus != AUDIO_JAVA_SUCCESS) {
goto exit;
}
for (size_t i = 0; i < sessions.size(); i++) {
const sp<AudioSessionInfo>& s = sessions.itemAt(i);
jobject jSession = env->NewObject(gAudioSessionInfoClass, gAudioSessionInfoCstor,
s->mSessionId, s->mStream, s->mFlags, s->mChannelMask, s->mUid);
if (jSession == NULL) {
jStatus = (jint)AUDIO_JAVA_ERROR;
goto exit;
}
env->CallBooleanMethod(jSessions, gArrayListMethods.add, jSession);
env->DeleteLocalRef(jSession);
}
exit:
return jStatus;
}
// ----------------------------------------------------------------------------
static JNINativeMethod gMethods[] = {
{"native_listAudioSessions", "(ILjava/util/ArrayList;)I",
(void *)org_cyanogenmod_platform_internal_CMAudioService_listAudioSessions},
{"native_registerAudioSessionCallback", "(Z)V",
(void *)org_cyanogenmod_platform_internal_CMAudioService_registerAudioSessionCallback},
};
int register_org_cyanogenmod_platform_internal_CMAudioService(JNIEnv *env)
{
jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList");
gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass);
gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
gArrayListMethods.toArray = GetMethodIDOrDie(env, arrayListClass, "toArray", "()[Ljava/lang/Object;");
jclass audioSessionInfoClass = FindClassOrDie(env, "cyanogenmod/media/AudioSessionInfo");
gAudioSessionInfoClass = MakeGlobalRefOrDie(env, audioSessionInfoClass);
gAudioSessionInfoCstor = GetMethodIDOrDie(env, audioSessionInfoClass, "<init>", "(IIIII)V");
gAudioSessionEventHandlerMethods.postAudioSessionEventFromNative =
GetMethodIDOrDie(env, env->FindClass(kClassPathName),
"audioSessionCallbackFromNative", "(ILcyanogenmod/media/AudioSessionInfo;Z)V");
return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
} /* namespace android */

View File

@ -1,161 +0,0 @@
/*
* Copyright (C) 2016 The CyanogenMod 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 org.cyanogenmod.platform.internal;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;
import com.android.server.SystemService;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import cyanogenmod.app.CMContextConstants;
import cyanogenmod.media.AudioSessionInfo;
import cyanogenmod.media.CMAudioManager;
import cyanogenmod.media.ICMAudioService;
import cyanogenmod.platform.Manifest;
public class CMAudioService extends CMSystemService {
private static final String TAG = "CMAudioService";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private final Context mContext;
private static final int AUDIO_STATUS_OK = 0;
//keep in sync with include/media/AudioPolicy.h
private final static int AUDIO_OUTPUT_SESSION_EFFECTS_UPDATE = 10;
public CMAudioService(Context context) {
super(context);
mContext = context;
}
@Override
public String getFeatureDeclaration() {
return CMContextConstants.Features.AUDIO;
}
@Override
public void onStart() {
if (!NativeHelper.isNativeLibraryAvailable()) {
Log.wtf(TAG, "CM Audio service started by system server by native library is" +
"unavailable. Service will be unavailable.");
return;
}
publishBinderService(CMContextConstants.CM_AUDIO_SERVICE, mBinder);
}
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_BOOT_COMPLETED) {
if (NativeHelper.isNativeLibraryAvailable()) {
native_registerAudioSessionCallback(true);
}
}
}
private final IBinder mBinder = new ICMAudioService.Stub() {
@Override
public List<AudioSessionInfo> listAudioSessions(int streamType) throws RemoteException {
final ArrayList<AudioSessionInfo> sessions = new ArrayList<AudioSessionInfo>();
if (!NativeHelper.isNativeLibraryAvailable()) {
// no sessions for u
return sessions;
}
int status = native_listAudioSessions(streamType, sessions);
if (status != AUDIO_STATUS_OK) {
Log.e(TAG, "Error retrieving audio sessions! status=" + status);
}
return sessions;
}
@Override
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
pw.println();
pw.println("CMAudio Service State:");
try {
List<AudioSessionInfo> sessions = listAudioSessions(-1);
if (sessions.size() > 0) {
pw.println(" Audio sessions:");
for (AudioSessionInfo info : sessions) {
pw.println(" " + info.toString());
}
} else {
pw.println(" No active audio sessions");
}
} catch (RemoteException e) {
// nothing
}
}
};
private void broadcastSessionChanged(boolean added, AudioSessionInfo sessionInfo) {
Intent i = new Intent(CMAudioManager.ACTION_AUDIO_SESSIONS_CHANGED);
i.putExtra(CMAudioManager.EXTRA_SESSION_INFO, sessionInfo);
i.putExtra(CMAudioManager.EXTRA_SESSION_ADDED, added);
sendBroadcastToAll(i, Manifest.permission.OBSERVE_AUDIO_SESSIONS);
}
private void sendBroadcastToAll(Intent intent, String receiverPermission) {
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
final long ident = Binder.clearCallingIdentity();
try {
if (DEBUG) Log.d(TAG, "Sending broadcast: " + intent.toString());
mContext.sendBroadcastAsUser(intent, UserHandle.ALL, receiverPermission);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
/*
* Handles events from JNI
*/
private synchronized void audioSessionCallbackFromNative(int event,
AudioSessionInfo sessionInfo, boolean added) {
switch (event) {
case AUDIO_OUTPUT_SESSION_EFFECTS_UPDATE:
broadcastSessionChanged(added, sessionInfo);
break;
default:
Log.e(TAG, "Unknown event " + event);
}
}
private native final void native_registerAudioSessionCallback(boolean enabled);
private native final int native_listAudioSessions(
int stream, ArrayList<AudioSessionInfo> sessions);
}

View File

@ -0,0 +1,130 @@
/**
* Copyright (C) 2016 The CyanogenMod 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 org.cyanogenmod.platform.internal;
import org.cyanogenmod.platform.internal.common.BrokeredServiceConnection;
import android.annotation.NonNull;
import android.annotation.SdkConstant;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.util.Slog;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import cyanogenmod.app.CMContextConstants;
import cyanogenmod.media.AudioSessionInfo;
import cyanogenmod.media.ICMAudioService;
public class CMAudioServiceBroker extends BrokerableCMSystemService<ICMAudioService> {
private static final String TAG = "CMAudioServiceBroker";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private final Context mContext;
private static final ComponentName TARGET_IMPLEMENTATION_COMPONENT =
new ComponentName("org.cyanogenmod.cmaudio.service",
"org.cyanogenmod.cmaudio.service.CMAudioService");
public CMAudioServiceBroker(Context context) {
super(context);
mContext = context;
}
@Override
public void onBootPhase(int phase) {
super.onBootPhase(phase);
if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
publishBinderService(CMContextConstants.CM_AUDIO_SERVICE, new BinderService());
}
}
@Override
public void onStart() {
if (DEBUG) Slog.d(TAG, "service started");
}
@Override
protected ICMAudioService getIBinderAsIInterface(@NonNull IBinder service) {
return ICMAudioService.Stub.asInterface(service);
}
@Override
protected ICMAudioService getDefaultImplementation() {
return mServiceStubForFailure;
}
@Override
protected ComponentName getServiceComponent() {
return TARGET_IMPLEMENTATION_COMPONENT;
}
private void checkPermission() {
mContext.enforceCallingOrSelfPermission(
cyanogenmod.platform.Manifest.permission.OBSERVE_AUDIO_SESSIONS, null);
}
@Override
public String getFeatureDeclaration() {
return CMContextConstants.Features.AUDIO;
}
private final ICMAudioService mServiceStubForFailure = new ICMAudioService.Stub() {
@Override
public List<AudioSessionInfo> listAudioSessions(int streamType) throws RemoteException {
checkPermission();
return Collections.emptyList();
}
};
private final class BinderService extends ICMAudioService.Stub {
@Override
public List<AudioSessionInfo> listAudioSessions(int streamType) throws RemoteException {
checkPermission();
return getBrokeredService().listAudioSessions(streamType);
}
@Override
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
pw.println();
pw.println("CMAudio Service State:");
try {
List<AudioSessionInfo> sessions = listAudioSessions(-1);
if (sessions.size() > 0) {
pw.println(" Audio sessions:");
for (AudioSessionInfo info : sessions) {
pw.println(" " + info.toString());
}
} else {
pw.println(" No active audio sessions");
}
} catch (RemoteException e) {
// nothing
}
}
}
}

View File

@ -24,7 +24,8 @@
<protected-broadcast android:name="cyanogenmod.intent.action.SCREEN_CAMERA_GESTURE" />
<protected-broadcast android:name="cyanogenmod.intent.action.INITIALIZE_CM_HARDWARE" />
<protected-broadcast android:name="cyanogenmod.intent.action.ACTION_AUDIO_SESSIONS_CHANGED" />
<protected-broadcast android:name="cyanogenmod.intent.action.ACTION_AUDIO_SESSIONS_CHANGED"
android:permission="cyanogenmod.permission.MANAGE_AUDIO_SESSIONS" />
<!-- Must be required by an, to ensure that only the system can bind to it.
@hide -->
@ -220,6 +221,13 @@
android:description="@string/permdesc_observe_audio_sessions"
android:protectionLevel="normal"/>
<!-- Allows an application to post system-wide changes to audio sessions
@hide -->
<permission android:name="cyanogenmod.permission.MANAGE_AUDIO_SESSIONS"
android:label="@string/permlab_manage_audio_sessions"
android:description="@string/permdesc_manage_audio_sessions"
android:protectionLevel="signature|privileged"/>
<!-- Allows an application to access the weather service.
<p>Although the protection is normal, this permission should be required ONLY by those apps
meant to do something meaningful with the data provided by the service (LockClock, SysUI)-->

View File

@ -112,7 +112,7 @@
<item>org.cyanogenmod.platform.internal.LiveLockScreenServiceBroker</item>
<item>org.cyanogenmod.platform.internal.CMWeatherManagerService</item>
<item>org.cyanogenmod.platform.internal.display.LiveDisplayService</item>
<item>org.cyanogenmod.platform.internal.CMAudioService</item>
<item>org.cyanogenmod.platform.internal.CMAudioServiceBroker</item>
</string-array>
<!-- The CMSystemServer class that is invoked from Android's SystemServer -->

View File

@ -218,6 +218,10 @@
<string name="permlab_observe_audio_sessions">observe audio session changes</string>
<string name="permdesc_observe_audio_sessions">Allows an app to observe audio streams being created and destroyed.</string>
<!-- CMAudioService - observe session changes permission -->
<string name="permlab_manage_audio_sessions">manage audio session changes</string>
<string name="permdesc_manage_audio_sessions">Allows an app to send audio stream updates.</string>
<!-- QuickSettings: Themes tile -->
<string name="qs_themes_label">Themes</string>
<string name="qs_themes_content_description">Customize your theme</string>