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

Commit bf68a16a authored by Amy Zhang's avatar Amy Zhang
Browse files

Add an initial TunerClient/FrontendClient interface to interact between Tuner HIDL and Tuner JNI

Test: make libmedia_tv_tuner
Bug: 174095851
Change-Id: I8f1b0c79860eb4bf8d4d16595e8b798ad691288a
parent 1bf2b0f2
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -546,7 +546,6 @@ public class Tuner implements AutoCloseable {
     */
    private native Frontend nativeOpenFrontendByHandle(int handle);
    @Result
    private native int nativeCloseFrontendByHandle(int handle);
    private native int nativeTune(int type, FrontendSettings settings);
    private native int nativeStopTune();
    private native int nativeScan(int settingsType, FrontendSettings settings, int scanType);
+4 −0
Original line number Diff line number Diff line
@@ -139,12 +139,15 @@ cc_library_shared {
    name: "libmedia_tv_tuner",
    srcs: [
        "android_media_tv_Tuner.cpp",
        "tuner/FrontendClient.cpp",
        "tuner/TunerClient.cpp",
    ],

    shared_libs: [
        "android.hardware.graphics.bufferqueue@2.0",
        "android.hardware.tv.tuner@1.0",
        "android.hardware.tv.tuner@1.1",
        "libbinder_ndk",
        "libandroid_runtime",
        "libcutils",
        "libfmq",
@@ -153,6 +156,7 @@ cc_library_shared {
        "libmedia",
        "libnativehelper",
        "libutils",
        "tv_tuner_aidl_interface-ndk_platform",
    ],
    defaults: [
        "libcodec2-impl-defaults",
+35 −67
Original line number Diff line number Diff line
@@ -1172,7 +1172,7 @@ Return<void> FrontendCallback::onScanMessageExt1_1(FrontendScanMessageTypeExt1_1

sp<ITuner> JTuner::mTuner;
sp<::android::hardware::tv::tuner::V1_1::ITuner> JTuner::mTuner_1_1;
int JTuner::mTunerVersion = 0;
sp<TunerClient> JTuner::mTunerClient;

JTuner::JTuner(JNIEnv *env, jobject thiz)
    : mClass(NULL) {
@@ -1181,9 +1181,13 @@ JTuner::JTuner(JNIEnv *env, jobject thiz)

    mClass = (jclass)env->NewGlobalRef(clazz);
    mObject = env->NewWeakGlobalRef(thiz);
    // TODO: remove after migrate to client lib
    if (mTuner == NULL) {
        mTuner = getTunerService();
    }
    if (mTunerClient == NULL) {
        mTunerClient = new TunerClient();
    }
}

JTuner::~JTuner() {
@@ -1198,13 +1202,13 @@ JTuner::~JTuner() {
    env->DeleteWeakGlobalRef(mObject);
    env->DeleteGlobalRef(mClass);
    mTuner = NULL;
    mTunerClient = NULL;
    mClass = NULL;
    mObject = NULL;
}

sp<ITuner> JTuner::getTunerService() {
    if (mTuner == nullptr) {
        mTunerVersion = 0;
        mTuner_1_1 = ::android::hardware::tv::tuner::V1_1::ITuner::getService();

        if (mTuner_1_1 == nullptr) {
@@ -1212,12 +1216,9 @@ sp<ITuner> JTuner::getTunerService() {
            mTuner = ITuner::getService();
            if (mTuner == nullptr) {
                ALOGW("Failed to get tuner 1.0 service.");
            } else {
                mTunerVersion = 1 << 16;
            }
        } else {
            mTuner = static_cast<sp<ITuner>>(mTuner_1_1);
            mTunerVersion = ((1 << 16) | 1);
         }
     }
     return mTuner;
@@ -1225,15 +1226,13 @@ sp<ITuner> JTuner::getTunerService() {

jint JTuner::getTunerVersion() {
    ALOGD("JTuner::getTunerVersion()");
    return (jint) mTunerVersion;
    return (jint) mTunerClient->getHalTunerVersion();
}

jobject JTuner::getFrontendIds() {
    ALOGD("JTuner::getFrontendIds()");
    mTuner->getFrontendIds([&](Result, const hidl_vec<FrontendId>& frontendIds) {
        mFeIds = frontendIds;
    });
    if (mFeIds.size() == 0) {
    vector<FrontendId> ids = mTunerClient->getFrontendIds();
    if (ids.size() == 0) {
        ALOGW("Frontend isn't available");
        return NULL;
    }
@@ -1246,8 +1245,8 @@ jobject JTuner::getFrontendIds() {
    jclass integerClazz = env->FindClass("java/lang/Integer");
    jmethodID intInit = env->GetMethodID(integerClazz, "<init>", "(I)V");

    for (int i=0; i < mFeIds.size(); i++) {
       jobject idObj = env->NewObject(integerClazz, intInit, mFeIds[i]);
    for (int i=0; i < ids.size(); i++) {
       jobject idObj = env->NewObject(integerClazz, intInit, ids[i]);
       env->CallBooleanMethod(obj, arrayListAdd, idObj);
    }
    return obj;
@@ -1284,14 +1283,6 @@ jobject JTuner::openFrontendById(int id) {
            (jint) jId);
}

jint JTuner::closeFrontendById(int id) {
    if (mFe != NULL && mFeId == id) {
        Result r = mFe->close();
        return (jint) r;
    }
    return (jint) Result::SUCCESS;
}

jobject JTuner::getAnalogFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps) {
    jclass clazz = env->FindClass("android/media/tv/tuner/frontend/AnalogFrontendCapabilities");
    jmethodID capsInit = env->GetMethodID(clazz, "<init>", "(II)V");
@@ -1402,37 +1393,26 @@ jobject JTuner::getDtmbFrontendCaps(JNIEnv *env, int id) {
    jclass clazz = env->FindClass("android/media/tv/tuner/frontend/DtmbFrontendCapabilities");
    jmethodID capsInit = env->GetMethodID(clazz, "<init>", "(IIIIII)V");

    if (mTuner_1_1 == NULL) {
        ALOGD("1.1 Tuner is not found. Dtmb Frontend Caps are not supported.");
    shared_ptr<FrontendDtmbCapabilities> dtmbCaps = mTunerClient->getFrontendDtmbCapabilities(id);
    if (dtmbCaps == NULL) {
        return NULL;
    }

    Result result;
    FrontendDtmbCapabilities dtmbCaps;
    mTuner_1_1->getFrontendDtmbCapabilities(id,
            [&](Result r, const FrontendDtmbCapabilities& caps) {
        dtmbCaps = caps;
        result = r;
    });
    jint modulationCap = dtmbCaps.modulationCap;
    jint transmissionModeCap = dtmbCaps.transmissionModeCap;
    jint guardIntervalCap = dtmbCaps.guardIntervalCap;
    jint interleaveModeCap = dtmbCaps.interleaveModeCap;
    jint codeRateCap = dtmbCaps.codeRateCap;
    jint bandwidthCap = dtmbCaps.bandwidthCap;
    jint modulationCap = dtmbCaps->modulationCap;
    jint transmissionModeCap = dtmbCaps->transmissionModeCap;
    jint guardIntervalCap = dtmbCaps->guardIntervalCap;
    jint interleaveModeCap = dtmbCaps->interleaveModeCap;
    jint codeRateCap = dtmbCaps->codeRateCap;
    jint bandwidthCap = dtmbCaps->bandwidthCap;

    return env->NewObject(clazz, capsInit, modulationCap, transmissionModeCap, guardIntervalCap,
            interleaveModeCap, codeRateCap, bandwidthCap);
}

jobject JTuner::getFrontendInfo(int id) {
    FrontendInfo feInfo;
    Result res;
    mTuner->getFrontendInfo(id, [&](Result r, const FrontendInfo& info) {
        feInfo = info;
        res = r;
    });
    if (res != Result::SUCCESS) {
    shared_ptr<FrontendInfo> feInfo;
    feInfo = mTunerClient->getFrontendInfo(id);
    if (feInfo == NULL) {
        return NULL;
    }

@@ -1441,30 +1421,27 @@ jobject JTuner::getFrontendInfo(int id) {
    jmethodID infoInit = env->GetMethodID(clazz, "<init>",
            "(IIIIIIII[ILandroid/media/tv/tuner/frontend/FrontendCapabilities;)V");

    jint type = (jint) feInfo.type;
    jint minFrequency = feInfo.minFrequency;
    jint maxFrequency = feInfo.maxFrequency;
    jint minSymbolRate = feInfo.minSymbolRate;
    jint maxSymbolRate = feInfo.maxSymbolRate;
    jint acquireRange = feInfo.acquireRange;
    jint exclusiveGroupId = feInfo.exclusiveGroupId;
    jintArray statusCaps = env->NewIntArray(feInfo.statusCaps.size());
    jint type = (jint) feInfo->type;
    jint minFrequency = feInfo->minFrequency;
    jint maxFrequency = feInfo->maxFrequency;
    jint minSymbolRate = feInfo->minSymbolRate;
    jint maxSymbolRate = feInfo->maxSymbolRate;
    jint acquireRange = feInfo->acquireRange;
    jint exclusiveGroupId = feInfo->exclusiveGroupId;
    jintArray statusCaps = env->NewIntArray(feInfo->statusCaps.size());
    env->SetIntArrayRegion(
            statusCaps, 0, feInfo.statusCaps.size(),
            reinterpret_cast<jint*>(&feInfo.statusCaps[0]));
    FrontendInfo::FrontendCapabilities caps = feInfo.frontendCaps;
            statusCaps, 0, feInfo->statusCaps.size(),
            reinterpret_cast<jint*>(&feInfo->statusCaps[0]));
    FrontendInfo::FrontendCapabilities caps = feInfo->frontendCaps;

    jobject jcaps = NULL;

    if (feInfo.type == static_cast<FrontendType>(
    if (feInfo->type == static_cast<FrontendType>(
            ::android::hardware::tv::tuner::V1_1::FrontendType::DTMB)) {
        if (mTuner_1_1 == NULL) {
            return NULL;
        }
        jcaps = getDtmbFrontendCaps(env, id);
    }

    switch(feInfo.type) {
    switch(feInfo->type) {
        case FrontendType::ANALOG:
            if (FrontendInfo::FrontendCapabilities::hidl_discriminator::analogCaps
                    == caps.getDiscriminator()) {
@@ -3426,13 +3403,6 @@ static jobject android_media_tv_Tuner_open_frontend_by_handle(
    return tuner->openFrontendById(id);
}

static jint android_media_tv_Tuner_close_frontend_by_handle(
        JNIEnv *env, jobject thiz, jint handle) {
    sp<JTuner> tuner = getTuner(env, thiz);
    uint32_t id = getResourceIdFromHandle(handle);
    return tuner->closeFrontendById(id);
}

static int android_media_tv_Tuner_tune(JNIEnv *env, jobject thiz, jint type, jobject settings) {
    sp<JTuner> tuner = getTuner(env, thiz);
    FrontendSettings setting = getFrontendSettings(env, type, settings);
@@ -4746,8 +4716,6 @@ static const JNINativeMethod gTunerMethods[] = {
            (void *)android_media_tv_Tuner_get_frontend_ids },
    { "nativeOpenFrontendByHandle", "(I)Landroid/media/tv/tuner/Tuner$Frontend;",
            (void *)android_media_tv_Tuner_open_frontend_by_handle },
    { "nativeCloseFrontendByHandle", "(I)I",
            (void *)android_media_tv_Tuner_close_frontend_by_handle },
    { "nativeTune", "(ILandroid/media/tv/tuner/frontend/FrontendSettings;)I",
            (void *)android_media_tv_Tuner_tune },
    { "nativeStopTune", "()I", (void *)android_media_tv_Tuner_stop_tune },
+2 −4
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
#include <utils/Mutex.h>
#include <utils/RefBase.h>

#include "tuner/TunerClient.h"
#include "jni.h"

using ::android::hardware::EventFlag;
@@ -259,10 +260,7 @@ private:
    jweak mObject;
    static sp<ITuner> mTuner;
    static sp<::android::hardware::tv::tuner::V1_1::ITuner> mTuner_1_1;
    // An integer that carries the Tuner version. The high 16 bits are the major version number
    // while the low 16 bits are the minor version. Default value is unknown version 0.
    static int mTunerVersion;
    hidl_vec<FrontendId> mFeIds;
    static sp<TunerClient> mTunerClient;
    sp<IFrontend> mFe;
    sp<::android::hardware::tv::tuner::V1_1::IFrontend> mFe_1_1;
    int mFeId;
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright 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.
 */

#define LOG_TAG "FrontendClient"

#include <android-base/logging.h>
#include <utils/Log.h>

#include "FrontendClient.h"

namespace android {

FrontendClient::FrontendClient(shared_ptr<ITunerFrontend> tunerFrontend) {
    mTunerFrontend = tunerFrontend;
}

FrontendClient::~FrontendClient() {
    mTunerFrontend = NULL;
    mFrontend = NULL;
    mFrontend_1_1 = NULL;
}

void FrontendClient::setHidlFrontend(sp<IFrontend> frontend) {
    mFrontend = frontend;
    mFrontend_1_1 = ::android::hardware::tv::tuner::V1_1::IFrontend::castFrom(mFrontend);
}
}
Loading