Create MediaTuner Java class and JNI
Test: make; acloud create;
Change-Id: Idac63c5ecf370e2b6b209e9e57d9453876b307d9
diff --git a/media/java/android/media/tv/tuner/Tuner.java b/media/java/android/media/tv/tuner/Tuner.java
new file mode 100644
index 0000000..0228dc9
--- /dev/null
+++ b/media/java/android/media/tv/tuner/Tuner.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2019 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 android.media.tv.tuner;
+
+/**
+ * Tuner is used to interact with tuner devices.
+ *
+ * @hide
+ */
+public final class Tuner implements AutoCloseable {
+ private static final String TAG = "MediaTvTuner";
+ private static final boolean DEBUG = false;
+
+ static {
+ System.loadLibrary("media_tv_tuner");
+ nativeInit();
+ }
+
+ public Tuner() {
+ nativeSetup();
+ }
+
+ private long mNativeContext; // used by native jMediaTuner
+
+ @Override
+ public void close() {}
+
+ /**
+ * Native Initialization.
+ */
+ private static native void nativeInit();
+
+ /**
+ * Native setup.
+ */
+ private native void nativeSetup();
+}
diff --git a/media/jni/Android.bp b/media/jni/Android.bp
index b4edabf..a596d89 100644
--- a/media/jni/Android.bp
+++ b/media/jni/Android.bp
@@ -123,6 +123,30 @@
],
}
+cc_library_shared {
+ name: "libmedia_tv_tuner",
+ srcs: [
+ "android_media_tv_Tuner.cpp",
+ ],
+
+ shared_libs: [
+ "android.hardware.tv.tuner@1.0",
+ "libandroid_runtime",
+ "liblog",
+ "libutils",
+ ],
+
+ export_include_dirs: ["."],
+
+ cflags: [
+ "-Wall",
+ "-Werror",
+ "-Wno-error=deprecated-declarations",
+ "-Wunused",
+ "-Wunreachable-code",
+ ],
+}
+
subdirs = [
"audioeffect",
"soundpool",
diff --git a/media/jni/android_media_tv_Tuner.cpp b/media/jni/android_media_tv_Tuner.cpp
new file mode 100644
index 0000000..d499eee
--- /dev/null
+++ b/media/jni/android_media_tv_Tuner.cpp
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2019 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_TAG "TvTuner-JNI"
+#include <utils/Log.h>
+
+#include "android_media_tv_Tuner.h"
+#include "android_runtime/AndroidRuntime.h"
+
+#include <android/hardware/tv/tuner/1.0/ITuner.h>
+#include <media/stagefright/foundation/ADebug.h>
+
+#pragma GCC diagnostic ignored "-Wunused-function"
+
+using ::android::hardware::tv::tuner::V1_0::ITuner;
+
+struct fields_t {
+ jfieldID context;
+};
+
+static fields_t gFields;
+
+namespace android {
+
+sp<ITuner> JTuner::mTuner;
+
+JTuner::JTuner(JNIEnv *env, jobject thiz)
+ : mClass(NULL) {
+ jclass clazz = env->GetObjectClass(thiz);
+ CHECK(clazz != NULL);
+
+ mClass = (jclass)env->NewGlobalRef(clazz);
+ mObject = env->NewWeakGlobalRef(thiz);
+ if (mTuner == NULL) {
+ mTuner = getTunerService();
+ }
+}
+
+JTuner::~JTuner() {
+ JNIEnv *env = AndroidRuntime::getJNIEnv();
+
+ env->DeleteGlobalRef(mClass);
+ mTuner = NULL;
+ mClass = NULL;
+ mObject = NULL;
+}
+
+sp<ITuner> JTuner::getTunerService() {
+ if (mTuner == nullptr) {
+ mTuner = ITuner::getService();
+
+ if (mTuner == nullptr) {
+ ALOGW("Failed to get tuner service.");
+ }
+ }
+ return mTuner;
+}
+
+} // namespace android
+
+////////////////////////////////////////////////////////////////////////////////
+
+using namespace android;
+
+static sp<JTuner> setTuner(JNIEnv *env, jobject thiz, const sp<JTuner> &tuner) {
+ sp<JTuner> old = (JTuner *)env->GetLongField(thiz, gFields.context);
+
+ if (tuner != NULL) {
+ tuner->incStrong(thiz);
+ }
+ if (old != NULL) {
+ old->decStrong(thiz);
+ }
+ env->SetLongField(thiz, gFields.context, (jlong)tuner.get());
+
+ return old;
+}
+
+static sp<JTuner> getTuner(JNIEnv *env, jobject thiz) {
+ return (JTuner *)env->GetLongField(thiz, gFields.context);
+}
+
+static void android_media_tv_Tuner_native_init(JNIEnv *env) {
+ jclass clazz = env->FindClass("android/media/tv/tuner/Tuner");
+ CHECK(clazz != NULL);
+
+ gFields.context = env->GetFieldID(clazz, "mNativeContext", "J");
+ CHECK(gFields.context != NULL);
+}
+
+static void android_media_tv_Tuner_native_setup(JNIEnv *env, jobject thiz) {
+ sp<JTuner> tuner = new JTuner(env, thiz);
+ setTuner(env,thiz, tuner);
+}
+
+static const JNINativeMethod gMethods[] = {
+ { "nativeInit", "()V", (void *)android_media_tv_Tuner_native_init },
+ { "nativeSetup", "()V", (void *)android_media_tv_Tuner_native_setup },
+};
+
+static int register_android_media_tv_Tuner(JNIEnv *env) {
+ return AndroidRuntime::registerNativeMethods(
+ env, "android/media/tv/tuner/Tuner", gMethods, NELEM(gMethods));
+}
+
+jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
+{
+ JNIEnv* env = NULL;
+ jint result = -1;
+
+ if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
+ ALOGE("ERROR: GetEnv failed\n");
+ return result;
+ }
+ assert(env != NULL);
+
+ if (register_android_media_tv_Tuner(env) != JNI_OK) {
+ ALOGE("ERROR: Tuner native registration failed\n");
+ return result;
+ }
+ return JNI_VERSION_1_4;
+}
diff --git a/media/jni/android_media_tv_Tuner.h b/media/jni/android_media_tv_Tuner.h
new file mode 100644
index 0000000..e7d5924
--- /dev/null
+++ b/media/jni/android_media_tv_Tuner.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2019 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.
+ */
+
+#ifndef _ANDROID_MEDIA_TV_TUNER_H_
+#define _ANDROID_MEDIA_TV_TUNER_H_
+
+#include <android/hardware/tv/tuner/1.0/ITuner.h>
+#include <utils/RefBase.h>
+
+#include "jni.h"
+
+using ::android::hardware::tv::tuner::V1_0::ITuner;
+
+namespace android {
+
+struct JTuner : public RefBase {
+ JTuner(JNIEnv *env, jobject thiz);
+ sp<ITuner> getTunerService();
+protected:
+ virtual ~JTuner();
+
+private:
+ jclass mClass;
+ jweak mObject;
+ static sp<ITuner> mTuner;
+};
+
+} // namespace android
+
+#endif // _ANDROID_MEDIA_TV_TUNER_H_