Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c276c59e authored by Mikhail Naganov's avatar Mikhail Naganov Committed by Paul McLean
Browse files

nativemidi: Prototype demonstrating native access to IMidiDeviceServer

Framework changes and a demo app
Comment and finalized Native MIDI API
Replaced fixed PortRegistry tables with std::map.
more error handling.
Removed not-very-useful MidiDeviceManager class.
Made Java API functions @hide.

Bug: 30252756

Test: Manual
Change-Id: Iae98e589f38ef6d625ff0842401193fe98c5d881
parent 56fee637
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -35,6 +35,10 @@ import java.io.IOException;
 * Instances of this class are created by {@link MidiManager#openDevice}.
 */
public final class MidiDevice implements Closeable {
    static {
        System.loadLibrary("media_jni");
    }

    private static final String TAG = "MidiDevice";

    private final MidiDeviceInfo mDeviceInfo;
@@ -43,6 +47,7 @@ public final class MidiDevice implements Closeable {
    private final IBinder mClientToken;
    private final IBinder mDeviceToken;
    private boolean mIsDeviceClosed;
    private boolean mIsMirroredToNative;

    private final CloseGuard mGuard = CloseGuard.get();

@@ -209,10 +214,45 @@ public final class MidiDevice implements Closeable {
        }
    }

    /**
     * Makes Midi Device available to the Native API
     * @hide
     */
    public void mirrorToNative() throws IOException {
        if (mIsDeviceClosed || mIsMirroredToNative) {
            return;
        }

        int result = mirrorToNative(mDeviceServer.asBinder(), mDeviceInfo.getId());
        if (result != 0) {
            throw new IOException("Failed mirroring to native: " + result);
        }

        mIsMirroredToNative = true;
    }

    /**
     * Makes Midi Device no longer available to the Native API
     * @hide
     */
    public void removeFromNative() throws IOException {
        if (!mIsMirroredToNative) {
            return;
        }

        int result = removeFromNative(mDeviceInfo.getId());
        if (result != 0) {
            throw new IOException("Failed removing from native: " + result);
        }

        mIsMirroredToNative = false;
    }

    @Override
    public void close() throws IOException {
        synchronized (mGuard) {
            if (!mIsDeviceClosed) {
                removeFromNative();
                mGuard.close();
                mIsDeviceClosed = true;
                try {
@@ -238,4 +278,7 @@ public final class MidiDevice implements Closeable {
    public String toString() {
        return ("MidiDevice: " + mDeviceInfo.toString());
    }

    private native int mirrorToNative(IBinder deviceServerBinder, int uid);
    private native int removeFromNative(int uid);
}
+3 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ LOCAL_SRC_FILES:= \
    android_mtp_MtpDatabase.cpp \
    android_mtp_MtpDevice.cpp \
    android_mtp_MtpServer.cpp \
    midi/android_media_midi_MidiDevice.cpp \

LOCAL_SHARED_LIBRARIES := \
    libandroid_runtime \
@@ -34,6 +35,7 @@ LOCAL_SHARED_LIBRARIES := \
    libbinder \
    libmedia \
    libmediadrm \
    libmidi \
    libskia \
    libui \
    liblog \
@@ -55,6 +57,7 @@ LOCAL_C_INCLUDES += \
    external/tremor/Tremor \
    frameworks/base/core/jni \
    frameworks/base/libs/hwui \
    frameworks/base/media/native \
    frameworks/av/media/libmedia \
    frameworks/av/media/libstagefright \
    frameworks/av/media/mtp \
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "Midi-JNI"

#include <android_util_Binder.h>
#include <midi/MidiDeviceRegistry.h>
#include <nativehelper/jni.h>
#include <utils/Log.h>

using namespace android;
using namespace android::media::midi;

extern "C" jint Java_android_media_midi_MidiDevice_mirrorToNative(
        JNIEnv *env, jobject thiz, jobject midiDeviceServer, jint id)
{
    (void)thiz;
    sp<IBinder> serverBinder = ibinderForJavaObject(env, midiDeviceServer);
    if (serverBinder.get() == NULL) {
        ALOGE("Could not obtain IBinder from passed jobject");
        return -EINVAL;
    }
    // return MidiDeviceManager::getInstance().addDevice(serverBinder, uid);
    return MidiDeviceRegistry::getInstance().addDevice(
               new BpMidiDeviceServer(serverBinder), id);
}

extern "C" jint Java_android_media_midi_MidiDevice_removeFromNative(
        JNIEnv *env, jobject thiz, jint uid)
{
    (void)env;
    (void)thiz;
    // return MidiDeviceManager::getInstance().removeDevice(uid);
    return MidiDeviceRegistry::getInstance().removeDevice(uid);
}
+21 −0
Original line number Diff line number Diff line
// Copyright (C) 2017 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.

// The headers module is in frameworks/media/native/midi/Android.bp.
ndk_library {
    name: "libmidi.ndk",
    symbol_file: "libmidi.map.txt",
    first_version: "26",
//    unversioned_until: "current",
}
+22 −0
Original line number Diff line number Diff line
LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \
	../../java/android/media/midi/IMidiDeviceServer.aidl \
	midi.cpp \
	MidiDeviceRegistry.cpp \
	MidiPortRegistry.cpp

LOCAL_AIDL_INCLUDES := \
	$(FRAMEWORKS_BASE_JAVA_SRC_DIRS) \
	frameworks/native/aidl/binder

LOCAL_CFLAGS += -Wall -Werror -O0

LOCAL_MODULE := libmidi
LOCAL_MODULE_TAGS := optional

LOCAL_SHARED_LIBRARIES := liblog libbinder libutils libmedia

include $(BUILD_SHARED_LIBRARY)
Loading