shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2020, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "TunerService" |
| 18 | |
| 19 | #include <android/binder_manager.h> |
| 20 | #include <utils/Log.h> |
Amy Zhang | 0f04c45 | 2020-10-30 13:36:44 -0700 | [diff] [blame] | 21 | #include "TunerFrontend.h" |
shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 22 | #include "TunerService.h" |
| 23 | |
Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 24 | using ::aidl::android::media::tv::tuner::TunerFrontendAnalogCapabilities; |
| 25 | using ::aidl::android::media::tv::tuner::TunerFrontendAtsc3Capabilities; |
| 26 | using ::aidl::android::media::tv::tuner::TunerFrontendAtscCapabilities; |
| 27 | using ::aidl::android::media::tv::tuner::TunerFrontendCableCapabilities; |
| 28 | using ::aidl::android::media::tv::tuner::TunerFrontendCapabilities; |
| 29 | using ::aidl::android::media::tv::tuner::TunerFrontendDvbsCapabilities; |
| 30 | using ::aidl::android::media::tv::tuner::TunerFrontendDvbtCapabilities; |
| 31 | using ::aidl::android::media::tv::tuner::TunerFrontendIsdbs3Capabilities; |
| 32 | using ::aidl::android::media::tv::tuner::TunerFrontendIsdbsCapabilities; |
| 33 | using ::aidl::android::media::tv::tuner::TunerFrontendIsdbtCapabilities; |
shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 34 | using ::android::hardware::hidl_vec; |
| 35 | using ::android::hardware::tv::tuner::V1_0::FrontendId; |
Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 36 | using ::android::hardware::tv::tuner::V1_0::FrontendType; |
shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 37 | using ::android::hardware::tv::tuner::V1_0::Result; |
| 38 | |
| 39 | namespace android { |
| 40 | |
| 41 | sp<ITuner> TunerService::mTuner; |
| 42 | |
| 43 | TunerService::TunerService() {} |
| 44 | TunerService::~TunerService() {} |
| 45 | |
| 46 | void TunerService::instantiate() { |
| 47 | std::shared_ptr<TunerService> service = |
| 48 | ::ndk::SharedRefBase::make<TunerService>(); |
| 49 | AServiceManager_addService(service->asBinder().get(), getServiceName()); |
Amy Zhang | 0f04c45 | 2020-10-30 13:36:44 -0700 | [diff] [blame] | 50 | mTuner = ITuner::getService(); |
| 51 | if (mTuner == nullptr) { |
| 52 | ALOGE("Failed to get ITuner service."); |
| 53 | } |
shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Status TunerService::getFrontendIds(std::vector<int32_t>* ids, int32_t* /* _aidl_return */) { |
| 57 | if (mTuner == nullptr) { |
Amy Zhang | 0f04c45 | 2020-10-30 13:36:44 -0700 | [diff] [blame] | 58 | ALOGE("ITuner service is not init."); |
| 59 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 60 | static_cast<int32_t>(Result::UNAVAILABLE)); |
shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 61 | } |
| 62 | hidl_vec<FrontendId> feIds; |
| 63 | Result res; |
| 64 | mTuner->getFrontendIds([&](Result r, const hidl_vec<FrontendId>& frontendIds) { |
| 65 | feIds = frontendIds; |
| 66 | res = r; |
| 67 | }); |
| 68 | if (res != Result::SUCCESS) { |
| 69 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 70 | } |
| 71 | ids->resize(feIds.size()); |
| 72 | std::copy(feIds.begin(), feIds.end(), ids->begin()); |
| 73 | |
Amy Zhang | 0f04c45 | 2020-10-30 13:36:44 -0700 | [diff] [blame] | 74 | return Status::ok(); |
shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 77 | Status TunerService::getFrontendInfo( |
| 78 | int32_t frontendHandle, TunerServiceFrontendInfo* _aidl_return) { |
| 79 | if (mTuner == nullptr) { |
Amy Zhang | 0f04c45 | 2020-10-30 13:36:44 -0700 | [diff] [blame] | 80 | ALOGE("ITuner service is not init."); |
| 81 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 82 | static_cast<int32_t>(Result::UNAVAILABLE)); |
Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | Result res; |
| 86 | FrontendInfo info; |
| 87 | int feId = getResourceIdFromHandle(frontendHandle); |
| 88 | mTuner->getFrontendInfo(feId, [&](Result r, const FrontendInfo& feInfo) { |
| 89 | info = feInfo; |
| 90 | res = r; |
| 91 | }); |
| 92 | if (res != Result::SUCCESS) { |
Amy Zhang | 0f04c45 | 2020-10-30 13:36:44 -0700 | [diff] [blame] | 93 | return Status::fromServiceSpecificError(static_cast<int32_t>(res)); |
Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TunerServiceFrontendInfo tunerInfo = convertToAidlFrontendInfo(feId, info); |
| 97 | *_aidl_return = tunerInfo; |
Amy Zhang | 0f04c45 | 2020-10-30 13:36:44 -0700 | [diff] [blame] | 98 | return Status::ok(); |
| 99 | } |
| 100 | |
| 101 | Status TunerService::openFrontend( |
| 102 | int32_t frontendHandle, std::shared_ptr<ITunerFrontend>* _aidl_return) { |
| 103 | if (mTuner == nullptr) { |
| 104 | ALOGE("ITuner service is not init."); |
| 105 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 106 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 107 | } |
| 108 | |
| 109 | *_aidl_return = ::ndk::SharedRefBase::make<TunerFrontend>(mTuner, frontendHandle); |
| 110 | return Status::ok(); |
Amy Zhang | 70de35a | 2020-10-12 20:13:16 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TunerServiceFrontendInfo TunerService::convertToAidlFrontendInfo(int feId, FrontendInfo halInfo) { |
| 114 | TunerServiceFrontendInfo info{ |
| 115 | .id = feId, |
| 116 | .type = (int)halInfo.type, |
| 117 | .minFrequency = (int)halInfo.minFrequency, |
| 118 | .maxFrequency = (int)halInfo.maxFrequency, |
| 119 | .minSymbolRate = (int)halInfo.minSymbolRate, |
| 120 | .maxSymbolRate = (int)halInfo.maxSymbolRate, |
| 121 | .acquireRange = (int)halInfo.acquireRange, |
| 122 | .exclusiveGroupId = (int)halInfo.exclusiveGroupId, |
| 123 | }; |
| 124 | for (int i = 0; i < halInfo.statusCaps.size(); i++) { |
| 125 | info.statusCaps.push_back((int)halInfo.statusCaps[i]); |
| 126 | } |
| 127 | |
| 128 | TunerFrontendCapabilities caps; |
| 129 | switch (halInfo.type) { |
| 130 | case FrontendType::ANALOG: { |
| 131 | TunerFrontendAnalogCapabilities analogCaps{ |
| 132 | .typeCap = (int)halInfo.frontendCaps.analogCaps().typeCap, |
| 133 | .sifStandardCap = (int)halInfo.frontendCaps.analogCaps().sifStandardCap, |
| 134 | }; |
| 135 | caps.set<TunerFrontendCapabilities::analogCaps>(analogCaps); |
| 136 | break; |
| 137 | } |
| 138 | case FrontendType::ATSC: { |
| 139 | TunerFrontendAtscCapabilities atscCaps{ |
| 140 | .modulationCap = (int)halInfo.frontendCaps.atscCaps().modulationCap, |
| 141 | }; |
| 142 | caps.set<TunerFrontendCapabilities::atscCaps>(atscCaps); |
| 143 | break; |
| 144 | } |
| 145 | case FrontendType::ATSC3: { |
| 146 | TunerFrontendAtsc3Capabilities atsc3Caps{ |
| 147 | .bandwidthCap = (int)halInfo.frontendCaps.atsc3Caps().bandwidthCap, |
| 148 | .modulationCap = (int)halInfo.frontendCaps.atsc3Caps().modulationCap, |
| 149 | .timeInterleaveModeCap = |
| 150 | (int)halInfo.frontendCaps.atsc3Caps().timeInterleaveModeCap, |
| 151 | .codeRateCap = (int)halInfo.frontendCaps.atsc3Caps().codeRateCap, |
| 152 | .demodOutputFormatCap = (int)halInfo.frontendCaps.atsc3Caps().demodOutputFormatCap, |
| 153 | .fecCap = (int)halInfo.frontendCaps.atsc3Caps().fecCap, |
| 154 | }; |
| 155 | caps.set<TunerFrontendCapabilities::atsc3Caps>(atsc3Caps); |
| 156 | break; |
| 157 | } |
| 158 | case FrontendType::DVBC: { |
| 159 | TunerFrontendCableCapabilities cableCaps{ |
| 160 | .modulationCap = (int)halInfo.frontendCaps.dvbcCaps().modulationCap, |
| 161 | .codeRateCap = (int)halInfo.frontendCaps.dvbcCaps().fecCap, |
| 162 | .annexCap = (int)halInfo.frontendCaps.dvbcCaps().annexCap, |
| 163 | }; |
| 164 | caps.set<TunerFrontendCapabilities::cableCaps>(cableCaps); |
| 165 | break; |
| 166 | } |
| 167 | case FrontendType::DVBS: { |
| 168 | TunerFrontendDvbsCapabilities dvbsCaps{ |
| 169 | .modulationCap = (int)halInfo.frontendCaps.dvbsCaps().modulationCap, |
| 170 | .codeRateCap = (long)halInfo.frontendCaps.dvbsCaps().innerfecCap, |
| 171 | .standard = (int)halInfo.frontendCaps.dvbsCaps().standard, |
| 172 | }; |
| 173 | caps.set<TunerFrontendCapabilities::dvbsCaps>(dvbsCaps); |
| 174 | break; |
| 175 | } |
| 176 | case FrontendType::DVBT: { |
| 177 | TunerFrontendDvbtCapabilities dvbtCaps{ |
| 178 | .transmissionModeCap = (int)halInfo.frontendCaps.dvbtCaps().transmissionModeCap, |
| 179 | .bandwidthCap = (int)halInfo.frontendCaps.dvbtCaps().bandwidthCap, |
| 180 | .constellationCap = (int)halInfo.frontendCaps.dvbtCaps().constellationCap, |
| 181 | .codeRateCap = (int)halInfo.frontendCaps.dvbtCaps().coderateCap, |
| 182 | .hierarchyCap = (int)halInfo.frontendCaps.dvbtCaps().hierarchyCap, |
| 183 | .guardIntervalCap = (int)halInfo.frontendCaps.dvbtCaps().guardIntervalCap, |
| 184 | .isT2Supported = (bool)halInfo.frontendCaps.dvbtCaps().isT2Supported, |
| 185 | .isMisoSupported = (bool)halInfo.frontendCaps.dvbtCaps().isMisoSupported, |
| 186 | }; |
| 187 | caps.set<TunerFrontendCapabilities::dvbtCaps>(dvbtCaps); |
| 188 | break; |
| 189 | } |
| 190 | case FrontendType::ISDBS: { |
| 191 | TunerFrontendIsdbsCapabilities isdbsCaps{ |
| 192 | .modulationCap = (int)halInfo.frontendCaps.isdbsCaps().modulationCap, |
| 193 | .codeRateCap = (int)halInfo.frontendCaps.isdbsCaps().coderateCap, |
| 194 | }; |
| 195 | caps.set<TunerFrontendCapabilities::isdbsCaps>(isdbsCaps); |
| 196 | break; |
| 197 | } |
| 198 | case FrontendType::ISDBS3: { |
| 199 | TunerFrontendIsdbs3Capabilities isdbs3Caps{ |
| 200 | .modulationCap = (int)halInfo.frontendCaps.isdbs3Caps().modulationCap, |
| 201 | .codeRateCap = (int)halInfo.frontendCaps.isdbs3Caps().coderateCap, |
| 202 | }; |
| 203 | caps.set<TunerFrontendCapabilities::isdbs3Caps>(isdbs3Caps); |
| 204 | break; |
| 205 | } |
| 206 | case FrontendType::ISDBT: { |
| 207 | TunerFrontendIsdbtCapabilities isdbtCaps{ |
| 208 | .modeCap = (int)halInfo.frontendCaps.isdbtCaps().modeCap, |
| 209 | .bandwidthCap = (int)halInfo.frontendCaps.isdbtCaps().bandwidthCap, |
| 210 | .modulationCap = (int)halInfo.frontendCaps.isdbtCaps().modulationCap, |
| 211 | .codeRateCap = (int)halInfo.frontendCaps.isdbtCaps().coderateCap, |
| 212 | .guardIntervalCap = (int)halInfo.frontendCaps.isdbtCaps().guardIntervalCap, |
| 213 | }; |
| 214 | caps.set<TunerFrontendCapabilities::isdbtCaps>(isdbtCaps); |
| 215 | break; |
| 216 | } |
| 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | info.caps = caps; |
| 222 | return info; |
| 223 | } |
shubang | 23aa3ac | 2020-09-07 18:56:28 -0700 | [diff] [blame] | 224 | } // namespace android |