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

Commit 093c5f34 authored by Hongguang's avatar Hongguang
Browse files

Add tuner legacy HIDL HAL support.

The AIDL HAL based solution will be the default implementation. The
tuner service will only create the legacy HIDL HAL based implementation
when the HIDL HAL is available.

Bug: 191825295
Test: atest android.media.tv.tuner.cts on both AIDL and HIDL HAL.
Test: sampletunertvinput
Change-Id: I8436ae80721ad943c60b5814b535f3b87a54ccda
parent 83b6427a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -36,9 +36,12 @@ cc_library {

    srcs: [
        "Tuner*.cpp",
        "hidl/Tuner*.cpp",
    ],

    shared_libs: [
        "android.hardware.tv.tuner@1.0",
        "android.hardware.tv.tuner@1.1",
        "android.hardware.tv.tuner-V1-ndk",
        "libbase",
        "libbinder",
@@ -79,6 +82,8 @@ cc_binary {
    ],

    shared_libs: [
        "android.hardware.tv.tuner@1.0",
        "android.hardware.tv.tuner@1.1",
        "android.hardware.tv.tuner-V1-ndk",
        "libbase",
        "libbinder",
+0 −3
Original line number Diff line number Diff line
@@ -88,9 +88,6 @@ public:
    };

private:
    bool isAudioFilter();
    bool isVideoFilter();

    shared_ptr<IFilter> mFilter;
    int32_t mId;
    int64_t mId64Bit;
+101 −0
Original line number Diff line number Diff line
/*
 * Copyright 2021 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 "TunerHelper.h"

#include <aidl/android/media/tv/tunerresourcemanager/ITunerResourceManager.h>
#include <android/binder_manager.h>
#include <android/content/pm/IPackageManagerNative.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>

using ::aidl::android::media::tv::tunerresourcemanager::ITunerResourceManager;
using ::android::defaultServiceManager;
using ::android::IBinder;
using ::android::interface_cast;
using ::android::IServiceManager;
using ::android::sp;
using ::android::binder::Status;
using ::android::content::pm::IPackageManagerNative;

namespace aidl {
namespace android {
namespace media {
namespace tv {
namespace tuner {

// System Feature defined in PackageManager
static const ::android::String16 FEATURE_TUNER(::android::String16("android.hardware.tv.tuner"));

int32_t TunerHelper::sResourceRequestCount = 0;

bool TunerHelper::checkTunerFeature() {
    sp<IServiceManager> serviceMgr = defaultServiceManager();
    sp<IPackageManagerNative> packageMgr;
    if (serviceMgr.get() == nullptr) {
        ALOGE("%s: Cannot find service manager", __func__);
        return false;
    }

    sp<IBinder> binder = serviceMgr->waitForService(String16("package_native"));
    packageMgr = interface_cast<IPackageManagerNative>(binder);
    if (packageMgr != nullptr) {
        bool hasFeature = false;
        Status status = packageMgr->hasSystemFeature(FEATURE_TUNER, 0, &hasFeature);
        if (!status.isOk()) {
            ALOGE("%s: hasSystemFeature failed: %s", __func__, status.exceptionMessage().c_str());
            return false;
        }
        if (!hasFeature) {
            ALOGD("Current device does not support tuner feaure.");
            return false;
        }
    } else {
        ALOGD("%s: Cannot find package manager.", __func__);
        return false;
    }

    return true;
}

// TODO: update Demux, Descrambler.
void TunerHelper::updateTunerResources(const vector<TunerFrontendInfo>& feInfos,
                                       const vector<int32_t>& lnbHandles) {
    ::ndk::SpAIBinder binder(AServiceManager_waitForService("tv_tuner_resource_mgr"));
    shared_ptr<ITunerResourceManager> tunerRM = ITunerResourceManager::fromBinder(binder);
    if (tunerRM == nullptr) {
        return;
    }

    tunerRM->setFrontendInfoList(feInfos);
    tunerRM->setLnbInfoList(lnbHandles);
}

// TODO: create a map between resource id and handles.
int TunerHelper::getResourceIdFromHandle(int resourceHandle, int /*type*/) {
    return (resourceHandle & 0x00ff0000) >> 16;
}

int TunerHelper::getResourceHandleFromId(int id, int resourceType) {
    // TODO: build up randomly generated id to handle mapping
    return (resourceType & 0x000000ff) << 24 | (id << 16) | (sResourceRequestCount++ & 0xffff);
}

}  // namespace tuner
}  // namespace tv
}  // namespace media
}  // namespace android
}  // namespace aidl
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright 2021 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_TUNERDVRHELPER_H
#define ANDROID_MEDIA_TUNERDVRHELPER_H

#include <aidl/android/media/tv/tunerresourcemanager/TunerFrontendInfo.h>
#include <utils/String16.h>

using ::aidl::android::media::tv::tunerresourcemanager::TunerFrontendInfo;
using ::android::String16;

using namespace std;

namespace aidl {
namespace android {
namespace media {
namespace tv {
namespace tuner {

const static int TUNER_HAL_VERSION_UNKNOWN = 0;
const static int TUNER_HAL_VERSION_1_0 = 1 << 16;
const static int TUNER_HAL_VERSION_1_1 = (1 << 16) | 1;
const static int TUNER_HAL_VERSION_2_0 = 2 << 16;

typedef enum {
    FRONTEND,
    LNB,
    DEMUX,
    DESCRAMBLER,
} TunerResourceType;

class TunerHelper {
public:
    static bool checkTunerFeature();

    // TODO: update Demux, Descrambler.
    static void updateTunerResources(const vector<TunerFrontendInfo>& feInfos,
                                     const vector<int32_t>& lnbHandles);
    // TODO: create a map between resource id and handles.
    static int getResourceIdFromHandle(int resourceHandle, int type);
    static int getResourceHandleFromId(int id, int resourceType);

private:
    static int32_t sResourceRequestCount;
};

}  // namespace tuner
}  // namespace tv
}  // namespace media
}  // namespace android
}  // namespace aidl

#endif  // ANDROID_MEDIA_TUNERDVRHELPER_H
+21 −63
Original line number Diff line number Diff line
@@ -24,30 +24,20 @@
#include <aidl/android/hardware/tv/tuner/IFrontend.h>
#include <aidl/android/hardware/tv/tuner/ILnb.h>
#include <aidl/android/hardware/tv/tuner/Result.h>
#include <aidl/android/media/tv/tunerresourcemanager/TunerFrontendInfo.h>
#include <android/binder_manager.h>
#include <android/content/pm/IPackageManagerNative.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>

#include "TunerDemux.h"
#include "TunerDescrambler.h"
#include "TunerFrontend.h"
#include "TunerHelper.h"
#include "TunerLnb.h"

using ::aidl::android::hardware::tv::tuner::IDemux;
using ::aidl::android::hardware::tv::tuner::IDescrambler;
using ::aidl::android::hardware::tv::tuner::IFrontend;
using ::aidl::android::hardware::tv::tuner::Result;
using ::aidl::android::media::tv::tunerresourcemanager::TunerFrontendInfo;
using ::android::defaultServiceManager;
using ::android::IBinder;
using ::android::interface_cast;
using ::android::IServiceManager;
using ::android::sp;
using ::android::String16;
using ::android::binder::Status;
using ::android::content::pm::IPackageManagerNative;

namespace aidl {
namespace android {
@@ -56,34 +46,11 @@ namespace tv {
namespace tuner {

TunerService::TunerService() {
    sp<IServiceManager> serviceMgr = defaultServiceManager();
    sp<IPackageManagerNative> packageMgr;
    if (serviceMgr.get() == nullptr) {
        ALOGE("%s: Cannot find service manager", __func__);
    if (!TunerHelper::checkTunerFeature()) {
        ALOGD("Device doesn't have tuner hardware.");
        return;
    } else {
        sp<IBinder> binder = serviceMgr->waitForService(String16("package_native"));
        packageMgr = interface_cast<IPackageManagerNative>(binder);
    }

    if (packageMgr != nullptr) {
        bool hasFeature = false;
        Status status = packageMgr->hasSystemFeature(FEATURE_TUNER, 0, &hasFeature);
        if (!status.isOk()) {
            ALOGE("%s: hasSystemFeature failed: %s", __func__, status.exceptionMessage().c_str());
            return;
        }
        if (!hasFeature) {
            ALOGD("Current device does not support tuner feaure.");
            return;
        }
    } else {
        ALOGD("%s: Cannot find package manager.", __func__);
        return;
    }

    ::ndk::SpAIBinder binder(AServiceManager_waitForService("tv_tuner_resource_mgr"));
    mTunerResourceManager = ITunerResourceManager::fromBinder(binder);
    updateTunerResources();
}

@@ -174,7 +141,7 @@ bool TunerService::hasITuner() {
                static_cast<int32_t>(Result::UNAVAILABLE));
    }

    int id = getResourceIdFromHandle(frontendHandle, FRONTEND);
    int id = TunerHelper::getResourceIdFromHandle(frontendHandle, FRONTEND);
    shared_ptr<IFrontend> frontend;
    auto status = mTuner->openFrontendById(id, &frontend);
    if (status.isOk()) {
@@ -192,7 +159,7 @@ bool TunerService::hasITuner() {
    }

    shared_ptr<ILnb> lnb;
    int id = getResourceIdFromHandle(lnbHandle, LNB);
    int id = TunerHelper::getResourceIdFromHandle(lnbHandle, LNB);
    auto status = mTuner->openLnbById(id, &lnb);
    if (status.isOk()) {
        *_aidl_return = ::ndk::SharedRefBase::make<TunerLnb>(lnb, id);
@@ -228,7 +195,7 @@ bool TunerService::hasITuner() {
    }

    shared_ptr<IDescrambler> descrambler;
    // int id = getResourceIdFromHandle(descramblerHandle, DESCRAMBLER);
    // int id = TunerHelper::getResourceIdFromHandle(descramblerHandle, DESCRAMBLER);
    auto status = mTuner->openDescrambler(&descrambler);
    if (status.isOk()) {
        *_aidl_return = ::ndk::SharedRefBase::make<TunerDescrambler>(descrambler);
@@ -237,31 +204,29 @@ bool TunerService::hasITuner() {
    return status;
}

::ndk::ScopedAStatus TunerService::getTunerHalVersion(int* _aidl_return) {
    hasITuner();
    *_aidl_return = mTunerVersion;
    return ::ndk::ScopedAStatus::ok();
}

void TunerService::updateTunerResources() {
    if (!hasITuner() || mTunerResourceManager == nullptr) {
    if (!hasITuner()) {
        ALOGE("Failed to updateTunerResources");
        return;
    }

    updateFrontendResources();
    updateLnbResources();
    // TODO: update Demux, Descrambler.
}

::ndk::ScopedAStatus TunerService::getTunerHalVersion(int* _aidl_return) {
    hasITuner();
    *_aidl_return = mTunerVersion;
    return ::ndk::ScopedAStatus::ok();
    TunerHelper::updateTunerResources(getTRMFrontendInfos(), getTRMLnbHandles());
}

void TunerService::updateFrontendResources() {
vector<TunerFrontendInfo> TunerService::getTRMFrontendInfos() {
    vector<TunerFrontendInfo> infos;
    vector<int32_t> ids;
    auto status = mTuner->getFrontendIds(&ids);
    if (!status.isOk()) {
        return;
        return infos;
    }

    vector<TunerFrontendInfo> infos;
    for (int i = 0; i < ids.size(); i++) {
        FrontendInfo frontendInfo;
        auto res = mTuner->getFrontendInfo(ids[i], &frontendInfo);
@@ -269,31 +234,24 @@ void TunerService::updateFrontendResources() {
            continue;
        }
        TunerFrontendInfo tunerFrontendInfo{
                .handle = getResourceHandleFromId((int)ids[i], FRONTEND),
                .handle = TunerHelper::getResourceHandleFromId((int)ids[i], FRONTEND),
                .type = static_cast<int>(frontendInfo.type),
                .exclusiveGroupId = frontendInfo.exclusiveGroupId,
        };
        infos.push_back(tunerFrontendInfo);
    }
    mTunerResourceManager->setFrontendInfoList(infos);
}

void TunerService::updateLnbResources() {
    vector<int32_t> handles = getLnbHandles();
    if (handles.size() == 0) {
        return;
    }
    mTunerResourceManager->setLnbInfoList(handles);
    return infos;
}

vector<int32_t> TunerService::getLnbHandles() {
vector<int32_t> TunerService::getTRMLnbHandles() {
    vector<int32_t> lnbHandles;
    if (mTuner != nullptr) {
        vector<int32_t> lnbIds;
        auto res = mTuner->getLnbIds(&lnbIds);
        if (res.isOk()) {
            for (int i = 0; i < lnbIds.size(); i++) {
                lnbHandles.push_back(getResourceHandleFromId(lnbIds[i], LNB));
                lnbHandles.push_back(TunerHelper::getResourceHandleFromId(lnbIds[i], LNB));
            }
        }
    }
Loading