Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2021, 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_NDEBUG 0 |
| 18 | #define LOG_TAG "TunerHidlService" |
| 19 | |
| 20 | #include "TunerHidlService.h" |
| 21 | |
Hongguang | 15758a8 | 2021-10-29 13:32:07 -0700 | [diff] [blame] | 22 | #include <aidl/android/hardware/tv/tuner/FrontendIsdbtTimeInterleaveMode.h> |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 23 | #include <aidl/android/hardware/tv/tuner/Result.h> |
| 24 | #include <android/binder_manager.h> |
Hongguang | 34a479e | 2021-10-04 16:14:47 -0700 | [diff] [blame] | 25 | #include <binder/IPCThreadState.h> |
| 26 | #include <binder/PermissionCache.h> |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | |
| 29 | #include "TunerHelper.h" |
| 30 | #include "TunerHidlDemux.h" |
| 31 | #include "TunerHidlDescrambler.h" |
| 32 | #include "TunerHidlFrontend.h" |
| 33 | #include "TunerHidlLnb.h" |
| 34 | |
| 35 | using ::aidl::android::hardware::tv::tuner::FrontendAnalogCapabilities; |
| 36 | using ::aidl::android::hardware::tv::tuner::FrontendAtsc3Capabilities; |
| 37 | using ::aidl::android::hardware::tv::tuner::FrontendAtscCapabilities; |
| 38 | using ::aidl::android::hardware::tv::tuner::FrontendCapabilities; |
| 39 | using ::aidl::android::hardware::tv::tuner::FrontendDtmbCapabilities; |
| 40 | using ::aidl::android::hardware::tv::tuner::FrontendDvbcCapabilities; |
| 41 | using ::aidl::android::hardware::tv::tuner::FrontendDvbsCapabilities; |
| 42 | using ::aidl::android::hardware::tv::tuner::FrontendDvbtCapabilities; |
| 43 | using ::aidl::android::hardware::tv::tuner::FrontendIsdbs3Capabilities; |
| 44 | using ::aidl::android::hardware::tv::tuner::FrontendIsdbsCapabilities; |
| 45 | using ::aidl::android::hardware::tv::tuner::FrontendIsdbtCapabilities; |
Hongguang | 15758a8 | 2021-10-29 13:32:07 -0700 | [diff] [blame] | 46 | using ::aidl::android::hardware::tv::tuner::FrontendIsdbtTimeInterleaveMode; |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 47 | using ::aidl::android::hardware::tv::tuner::FrontendType; |
| 48 | using ::aidl::android::hardware::tv::tuner::Result; |
| 49 | using ::aidl::android::media::tv::tunerresourcemanager::TunerFrontendInfo; |
Hongguang | 34a479e | 2021-10-04 16:14:47 -0700 | [diff] [blame] | 50 | using ::android::IPCThreadState; |
| 51 | using ::android::PermissionCache; |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 52 | using ::android::hardware::hidl_vec; |
| 53 | |
| 54 | using HidlFrontendId = ::android::hardware::tv::tuner::V1_0::FrontendId; |
| 55 | using HidlLnbId = ::android::hardware::tv::tuner::V1_0::LnbId; |
| 56 | using HidlFrontendType = ::android::hardware::tv::tuner::V1_1::FrontendType; |
| 57 | |
| 58 | using namespace std; |
| 59 | |
| 60 | namespace aidl { |
| 61 | namespace android { |
| 62 | namespace media { |
| 63 | namespace tv { |
| 64 | namespace tuner { |
| 65 | |
Hongguang | 34a479e | 2021-10-04 16:14:47 -0700 | [diff] [blame] | 66 | shared_ptr<TunerHidlService> TunerHidlService::sTunerService = nullptr; |
| 67 | |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 68 | TunerHidlService::TunerHidlService() { |
| 69 | if (!TunerHelper::checkTunerFeature()) { |
| 70 | ALOGD("Device doesn't have tuner hardware."); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | updateTunerResources(); |
| 75 | } |
| 76 | |
Hongguang | d8ccaae | 2021-12-13 18:07:10 -0800 | [diff] [blame] | 77 | TunerHidlService::~TunerHidlService() { |
| 78 | mOpenedFrontends.clear(); |
| 79 | mLnaStatus = -1; |
| 80 | } |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 81 | |
| 82 | binder_status_t TunerHidlService::instantiate() { |
| 83 | if (HidlITuner::getService() == nullptr) { |
| 84 | ALOGD("Failed to get ITuner HIDL HAL"); |
| 85 | return STATUS_NAME_NOT_FOUND; |
| 86 | } |
| 87 | |
Hongguang | 34a479e | 2021-10-04 16:14:47 -0700 | [diff] [blame] | 88 | sTunerService = ::ndk::SharedRefBase::make<TunerHidlService>(); |
| 89 | return AServiceManager_addService(sTunerService->asBinder().get(), getServiceName()); |
| 90 | } |
| 91 | |
| 92 | shared_ptr<TunerHidlService> TunerHidlService::getTunerService() { |
| 93 | return sTunerService; |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool TunerHidlService::hasITuner() { |
| 97 | ALOGV("hasITuner"); |
| 98 | if (mTuner != nullptr) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | mTuner = HidlITuner::getService(); |
| 103 | if (mTuner == nullptr) { |
| 104 | ALOGE("Failed to get ITuner service"); |
| 105 | return false; |
| 106 | } |
| 107 | mTunerVersion = TUNER_HAL_VERSION_1_0; |
| 108 | |
| 109 | mTuner_1_1 = ::android::hardware::tv::tuner::V1_1::ITuner::castFrom(mTuner); |
| 110 | if (mTuner_1_1 != nullptr) { |
| 111 | mTunerVersion = TUNER_HAL_VERSION_1_1; |
| 112 | } else { |
| 113 | ALOGD("Failed to get ITuner_1_1 service"); |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool TunerHidlService::hasITuner_1_1() { |
| 120 | ALOGV("hasITuner_1_1"); |
| 121 | hasITuner(); |
| 122 | return (mTunerVersion == TUNER_HAL_VERSION_1_1); |
| 123 | } |
| 124 | |
| 125 | ::ndk::ScopedAStatus TunerHidlService::openDemux(int32_t /* in_demuxHandle */, |
| 126 | shared_ptr<ITunerDemux>* _aidl_return) { |
| 127 | ALOGV("openDemux"); |
| 128 | if (!hasITuner()) { |
| 129 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 130 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 131 | } |
| 132 | |
| 133 | HidlResult res; |
| 134 | uint32_t id; |
| 135 | sp<IDemux> demuxSp = nullptr; |
| 136 | mTuner->openDemux([&](HidlResult r, uint32_t demuxId, const sp<IDemux>& demux) { |
| 137 | demuxSp = demux; |
| 138 | id = demuxId; |
| 139 | res = r; |
| 140 | ALOGD("open demux, id = %d", demuxId); |
| 141 | }); |
| 142 | if (res == HidlResult::SUCCESS) { |
| 143 | *_aidl_return = ::ndk::SharedRefBase::make<TunerHidlDemux>(demuxSp, id); |
| 144 | return ::ndk::ScopedAStatus::ok(); |
| 145 | } |
| 146 | |
| 147 | ALOGW("open demux failed, res = %d", res); |
| 148 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 149 | } |
| 150 | |
| 151 | ::ndk::ScopedAStatus TunerHidlService::getDemuxCaps(DemuxCapabilities* _aidl_return) { |
| 152 | ALOGV("getDemuxCaps"); |
| 153 | if (!hasITuner()) { |
| 154 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 155 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 156 | } |
| 157 | |
| 158 | HidlResult res; |
| 159 | HidlDemuxCapabilities caps; |
| 160 | mTuner->getDemuxCaps([&](HidlResult r, const HidlDemuxCapabilities& demuxCaps) { |
| 161 | caps = demuxCaps; |
| 162 | res = r; |
| 163 | }); |
| 164 | if (res == HidlResult::SUCCESS) { |
| 165 | *_aidl_return = getAidlDemuxCaps(caps); |
| 166 | return ::ndk::ScopedAStatus::ok(); |
| 167 | } |
| 168 | |
| 169 | ALOGW("Get demux caps failed, res = %d", res); |
| 170 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 171 | } |
| 172 | |
| 173 | ::ndk::ScopedAStatus TunerHidlService::getFrontendIds(vector<int32_t>* ids) { |
| 174 | if (!hasITuner()) { |
| 175 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 176 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 177 | } |
| 178 | |
| 179 | hidl_vec<HidlFrontendId> feIds; |
| 180 | HidlResult res = getHidlFrontendIds(feIds); |
| 181 | if (res != HidlResult::SUCCESS) { |
| 182 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 183 | } |
| 184 | ids->resize(feIds.size()); |
| 185 | copy(feIds.begin(), feIds.end(), ids->begin()); |
| 186 | |
| 187 | return ::ndk::ScopedAStatus::ok(); |
| 188 | } |
| 189 | |
| 190 | ::ndk::ScopedAStatus TunerHidlService::getFrontendInfo(int32_t id, FrontendInfo* _aidl_return) { |
| 191 | if (!hasITuner()) { |
| 192 | ALOGE("ITuner service is not init."); |
| 193 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 194 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 195 | } |
| 196 | |
| 197 | HidlFrontendInfo info; |
| 198 | HidlResult res = getHidlFrontendInfo(id, info); |
| 199 | if (res != HidlResult::SUCCESS) { |
| 200 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 201 | } |
| 202 | |
| 203 | HidlFrontendDtmbCapabilities dtmbCaps; |
| 204 | if (static_cast<HidlFrontendType>(info.type) == HidlFrontendType::DTMB) { |
| 205 | if (!hasITuner_1_1()) { |
| 206 | ALOGE("ITuner_1_1 service is not init."); |
| 207 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 208 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 209 | } |
| 210 | |
| 211 | mTuner_1_1->getFrontendDtmbCapabilities( |
| 212 | id, [&](HidlResult r, const HidlFrontendDtmbCapabilities& caps) { |
| 213 | dtmbCaps = caps; |
| 214 | res = r; |
| 215 | }); |
| 216 | if (res != HidlResult::SUCCESS) { |
| 217 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res)); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | *_aidl_return = getAidlFrontendInfo(info, dtmbCaps); |
| 222 | return ::ndk::ScopedAStatus::ok(); |
| 223 | } |
| 224 | |
| 225 | ::ndk::ScopedAStatus TunerHidlService::openFrontend(int32_t frontendHandle, |
| 226 | shared_ptr<ITunerFrontend>* _aidl_return) { |
| 227 | if (!hasITuner()) { |
| 228 | ALOGE("ITuner service is not init."); |
| 229 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 230 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 231 | } |
| 232 | |
| 233 | HidlResult status; |
| 234 | sp<HidlIFrontend> frontend; |
| 235 | int id = TunerHelper::getResourceIdFromHandle(frontendHandle, FRONTEND); |
| 236 | mTuner->openFrontendById(id, [&](HidlResult result, const sp<HidlIFrontend>& fe) { |
| 237 | frontend = fe; |
| 238 | status = result; |
| 239 | }); |
| 240 | if (status != HidlResult::SUCCESS) { |
| 241 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(status)); |
| 242 | } |
Hongguang | d8ccaae | 2021-12-13 18:07:10 -0800 | [diff] [blame] | 243 | |
| 244 | shared_ptr<TunerHidlFrontend> tunerFrontend = |
| 245 | ::ndk::SharedRefBase::make<TunerHidlFrontend>(frontend, id); |
| 246 | if (mLnaStatus != -1) { |
| 247 | tunerFrontend->setLna(mLnaStatus == 1); |
| 248 | } |
| 249 | { |
| 250 | Mutex::Autolock _l(mOpenedFrontendsLock); |
| 251 | mOpenedFrontends.insert(tunerFrontend); |
| 252 | } |
| 253 | *_aidl_return = tunerFrontend; |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 254 | return ::ndk::ScopedAStatus::ok(); |
| 255 | } |
| 256 | |
| 257 | ::ndk::ScopedAStatus TunerHidlService::openLnb(int lnbHandle, shared_ptr<ITunerLnb>* _aidl_return) { |
| 258 | if (!hasITuner()) { |
| 259 | ALOGD("get ITuner failed"); |
| 260 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 261 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 262 | } |
| 263 | |
| 264 | HidlResult status; |
| 265 | sp<HidlILnb> lnb; |
| 266 | int id = TunerHelper::getResourceIdFromHandle(lnbHandle, LNB); |
| 267 | mTuner->openLnbById(id, [&](HidlResult result, const sp<HidlILnb>& lnbSp) { |
| 268 | lnb = lnbSp; |
| 269 | status = result; |
| 270 | }); |
| 271 | if (status != HidlResult::SUCCESS) { |
| 272 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(status)); |
| 273 | } |
| 274 | |
| 275 | *_aidl_return = ::ndk::SharedRefBase::make<TunerHidlLnb>(lnb, id); |
| 276 | return ::ndk::ScopedAStatus::ok(); |
| 277 | } |
| 278 | |
| 279 | ::ndk::ScopedAStatus TunerHidlService::openLnbByName(const string& lnbName, |
| 280 | shared_ptr<ITunerLnb>* _aidl_return) { |
| 281 | if (!hasITuner()) { |
| 282 | ALOGE("get ITuner failed"); |
| 283 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 284 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 285 | } |
| 286 | |
| 287 | int lnbId; |
| 288 | HidlResult status; |
| 289 | sp<HidlILnb> lnb; |
| 290 | mTuner->openLnbByName(lnbName, [&](HidlResult r, HidlLnbId id, const sp<HidlILnb>& lnbSp) { |
| 291 | status = r; |
| 292 | lnb = lnbSp; |
| 293 | lnbId = static_cast<int32_t>(id); |
| 294 | }); |
| 295 | if (status != HidlResult::SUCCESS) { |
| 296 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(status)); |
| 297 | } |
| 298 | |
| 299 | *_aidl_return = ::ndk::SharedRefBase::make<TunerHidlLnb>(lnb, lnbId); |
| 300 | return ::ndk::ScopedAStatus::ok(); |
| 301 | } |
| 302 | |
| 303 | ::ndk::ScopedAStatus TunerHidlService::openDescrambler( |
| 304 | int32_t /*descramblerHandle*/, shared_ptr<ITunerDescrambler>* _aidl_return) { |
| 305 | if (!hasITuner()) { |
| 306 | ALOGD("get ITuner failed"); |
| 307 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 308 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 309 | } |
| 310 | |
| 311 | HidlResult status; |
| 312 | sp<HidlIDescrambler> descrambler; |
| 313 | //int id = TunerHelper::getResourceIdFromHandle(descramblerHandle, DESCRAMBLER); |
| 314 | mTuner->openDescrambler([&](HidlResult r, const sp<HidlIDescrambler>& descramblerSp) { |
| 315 | status = r; |
| 316 | descrambler = descramblerSp; |
| 317 | }); |
| 318 | if (status != HidlResult::SUCCESS) { |
| 319 | return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(status)); |
| 320 | } |
| 321 | |
| 322 | *_aidl_return = ::ndk::SharedRefBase::make<TunerHidlDescrambler>(descrambler); |
| 323 | return ::ndk::ScopedAStatus::ok(); |
| 324 | } |
| 325 | |
| 326 | ::ndk::ScopedAStatus TunerHidlService::getTunerHalVersion(int* _aidl_return) { |
| 327 | hasITuner(); |
| 328 | *_aidl_return = mTunerVersion; |
| 329 | return ::ndk::ScopedAStatus::ok(); |
| 330 | } |
| 331 | |
Hongguang | 34a479e | 2021-10-04 16:14:47 -0700 | [diff] [blame] | 332 | ::ndk::ScopedAStatus TunerHidlService::openSharedFilter( |
| 333 | const string& in_filterToken, const shared_ptr<ITunerFilterCallback>& in_cb, |
| 334 | shared_ptr<ITunerFilter>* _aidl_return) { |
| 335 | if (!hasITuner()) { |
| 336 | ALOGE("get ITuner failed"); |
| 337 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 338 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 339 | } |
| 340 | |
| 341 | if (!PermissionCache::checkCallingPermission(sSharedFilterPermission)) { |
| 342 | ALOGE("Request requires android.permission.ACCESS_TV_SHARED_FILTER"); |
| 343 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 344 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 345 | } |
| 346 | |
| 347 | Mutex::Autolock _l(mSharedFiltersLock); |
| 348 | if (mSharedFilters.find(in_filterToken) == mSharedFilters.end()) { |
| 349 | *_aidl_return = nullptr; |
| 350 | ALOGD("fail to find %s", in_filterToken.c_str()); |
| 351 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 352 | static_cast<int32_t>(Result::INVALID_STATE)); |
| 353 | } |
| 354 | |
| 355 | shared_ptr<TunerHidlFilter> filter = mSharedFilters.at(in_filterToken); |
| 356 | IPCThreadState* ipc = IPCThreadState::self(); |
| 357 | const int pid = ipc->getCallingPid(); |
| 358 | if (!filter->isSharedFilterAllowed(pid)) { |
| 359 | *_aidl_return = nullptr; |
| 360 | ALOGD("shared filter %s is opened in the same process", in_filterToken.c_str()); |
| 361 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 362 | static_cast<int32_t>(Result::INVALID_STATE)); |
| 363 | } |
| 364 | |
| 365 | filter->attachSharedFilterCallback(in_cb); |
| 366 | |
| 367 | *_aidl_return = filter; |
| 368 | return ::ndk::ScopedAStatus::ok(); |
| 369 | } |
| 370 | |
Hongguang | d8ccaae | 2021-12-13 18:07:10 -0800 | [diff] [blame] | 371 | ::ndk::ScopedAStatus TunerHidlService::setLna(bool bEnable) { |
| 372 | if (!hasITuner()) { |
| 373 | ALOGE("get ITuner failed"); |
| 374 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 375 | static_cast<int32_t>(Result::UNAVAILABLE)); |
| 376 | } |
| 377 | |
| 378 | mLnaStatus = bEnable ? 1 : 0; |
| 379 | |
| 380 | { |
| 381 | Mutex::Autolock _l(mOpenedFrontendsLock); |
| 382 | for (auto it = mOpenedFrontends.begin(); it != mOpenedFrontends.end(); ++it) { |
| 383 | (*it)->setLna(mLnaStatus == 1); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return ::ndk::ScopedAStatus::ok(); |
| 388 | } |
| 389 | |
Hongguang | 34a479e | 2021-10-04 16:14:47 -0700 | [diff] [blame] | 390 | string TunerHidlService::addFilterToShared(const shared_ptr<TunerHidlFilter>& sharedFilter) { |
| 391 | Mutex::Autolock _l(mSharedFiltersLock); |
| 392 | |
| 393 | // Use sharedFilter address as token. |
| 394 | string token = to_string(reinterpret_cast<std::uintptr_t>(sharedFilter.get())); |
| 395 | mSharedFilters[token] = sharedFilter; |
| 396 | |
| 397 | return token; |
| 398 | } |
| 399 | |
| 400 | void TunerHidlService::removeSharedFilter(const shared_ptr<TunerHidlFilter>& sharedFilter) { |
| 401 | Mutex::Autolock _l(mSharedFiltersLock); |
| 402 | |
| 403 | // Use sharedFilter address as token. |
| 404 | mSharedFilters.erase(to_string(reinterpret_cast<std::uintptr_t>(sharedFilter.get()))); |
| 405 | } |
| 406 | |
Hongguang | d8ccaae | 2021-12-13 18:07:10 -0800 | [diff] [blame] | 407 | void TunerHidlService::removeFrontend(const shared_ptr<TunerHidlFrontend>& frontend) { |
| 408 | Mutex::Autolock _l(mOpenedFrontendsLock); |
| 409 | for (auto it = mOpenedFrontends.begin(); it != mOpenedFrontends.end(); ++it) { |
| 410 | if (it->get() == frontend.get()) { |
| 411 | mOpenedFrontends.erase(it); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 417 | void TunerHidlService::updateTunerResources() { |
| 418 | if (!hasITuner()) { |
| 419 | ALOGE("Failed to updateTunerResources"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | TunerHelper::updateTunerResources(getTRMFrontendInfos(), getTRMLnbHandles()); |
| 424 | } |
| 425 | |
| 426 | vector<TunerFrontendInfo> TunerHidlService::getTRMFrontendInfos() { |
| 427 | vector<TunerFrontendInfo> infos; |
| 428 | hidl_vec<HidlFrontendId> ids; |
| 429 | HidlResult res = getHidlFrontendIds(ids); |
| 430 | if (res != HidlResult::SUCCESS) { |
| 431 | return infos; |
| 432 | } |
| 433 | |
| 434 | for (int i = 0; i < ids.size(); i++) { |
| 435 | HidlFrontendInfo frontendInfo; |
| 436 | HidlResult res = getHidlFrontendInfo(static_cast<int32_t>(ids[i]), frontendInfo); |
| 437 | if (res != HidlResult::SUCCESS) { |
| 438 | continue; |
| 439 | } |
| 440 | TunerFrontendInfo tunerFrontendInfo{ |
| 441 | .handle = TunerHelper::getResourceHandleFromId(static_cast<int32_t>(ids[i]), |
| 442 | FRONTEND), |
| 443 | .type = static_cast<int32_t>(frontendInfo.type), |
| 444 | .exclusiveGroupId = static_cast<int32_t>(frontendInfo.exclusiveGroupId), |
| 445 | }; |
| 446 | infos.push_back(tunerFrontendInfo); |
| 447 | } |
| 448 | |
| 449 | return infos; |
| 450 | } |
| 451 | |
| 452 | vector<int32_t> TunerHidlService::getTRMLnbHandles() { |
| 453 | vector<int32_t> lnbHandles; |
| 454 | if (mTuner != nullptr) { |
| 455 | HidlResult res; |
| 456 | vector<HidlLnbId> lnbIds; |
| 457 | mTuner->getLnbIds([&](HidlResult r, const hidl_vec<HidlLnbId>& ids) { |
| 458 | lnbIds = ids; |
| 459 | res = r; |
| 460 | }); |
| 461 | if (res == HidlResult::SUCCESS && lnbIds.size() > 0) { |
| 462 | for (int i = 0; i < lnbIds.size(); i++) { |
| 463 | lnbHandles.push_back( |
| 464 | TunerHelper::getResourceHandleFromId(static_cast<int32_t>(lnbIds[i]), LNB)); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return lnbHandles; |
| 470 | } |
| 471 | |
| 472 | HidlResult TunerHidlService::getHidlFrontendIds(hidl_vec<HidlFrontendId>& ids) { |
| 473 | if (mTuner == nullptr) { |
| 474 | return HidlResult::NOT_INITIALIZED; |
| 475 | } |
| 476 | HidlResult res; |
| 477 | mTuner->getFrontendIds([&](HidlResult r, const hidl_vec<HidlFrontendId>& frontendIds) { |
| 478 | ids = frontendIds; |
| 479 | res = r; |
| 480 | }); |
| 481 | return res; |
| 482 | } |
| 483 | |
| 484 | HidlResult TunerHidlService::getHidlFrontendInfo(const int id, HidlFrontendInfo& info) { |
| 485 | if (mTuner == nullptr) { |
| 486 | return HidlResult::NOT_INITIALIZED; |
| 487 | } |
| 488 | HidlResult res; |
| 489 | mTuner->getFrontendInfo(id, [&](HidlResult r, const HidlFrontendInfo& feInfo) { |
| 490 | info = feInfo; |
| 491 | res = r; |
| 492 | }); |
| 493 | return res; |
| 494 | } |
| 495 | |
| 496 | DemuxCapabilities TunerHidlService::getAidlDemuxCaps(const HidlDemuxCapabilities& caps) { |
| 497 | DemuxCapabilities aidlCaps{ |
| 498 | .numDemux = static_cast<int32_t>(caps.numDemux), |
| 499 | .numRecord = static_cast<int32_t>(caps.numRecord), |
| 500 | .numPlayback = static_cast<int32_t>(caps.numPlayback), |
| 501 | .numTsFilter = static_cast<int32_t>(caps.numTsFilter), |
| 502 | .numSectionFilter = static_cast<int32_t>(caps.numSectionFilter), |
| 503 | .numAudioFilter = static_cast<int32_t>(caps.numAudioFilter), |
| 504 | .numVideoFilter = static_cast<int32_t>(caps.numVideoFilter), |
| 505 | .numPesFilter = static_cast<int32_t>(caps.numPesFilter), |
| 506 | .numPcrFilter = static_cast<int32_t>(caps.numPcrFilter), |
| 507 | .numBytesInSectionFilter = static_cast<int64_t>(caps.numBytesInSectionFilter), |
| 508 | .filterCaps = static_cast<int32_t>(caps.filterCaps), |
| 509 | .bTimeFilter = caps.bTimeFilter, |
| 510 | }; |
| 511 | aidlCaps.linkCaps.resize(caps.linkCaps.size()); |
| 512 | copy(caps.linkCaps.begin(), caps.linkCaps.end(), aidlCaps.linkCaps.begin()); |
| 513 | return aidlCaps; |
| 514 | } |
| 515 | |
| 516 | FrontendInfo TunerHidlService::getAidlFrontendInfo( |
| 517 | const HidlFrontendInfo& halInfo, const HidlFrontendDtmbCapabilities& halDtmbCaps) { |
| 518 | FrontendInfo info{ |
| 519 | .type = static_cast<FrontendType>(halInfo.type), |
| 520 | .minFrequency = static_cast<int64_t>(halInfo.minFrequency), |
| 521 | .maxFrequency = static_cast<int64_t>(halInfo.maxFrequency), |
| 522 | .minSymbolRate = static_cast<int32_t>(halInfo.minSymbolRate), |
| 523 | .maxSymbolRate = static_cast<int32_t>(halInfo.maxSymbolRate), |
| 524 | .acquireRange = static_cast<int64_t>(halInfo.acquireRange), |
| 525 | .exclusiveGroupId = static_cast<int32_t>(halInfo.exclusiveGroupId), |
| 526 | }; |
| 527 | for (int i = 0; i < halInfo.statusCaps.size(); i++) { |
| 528 | info.statusCaps.push_back(static_cast<FrontendStatusType>(halInfo.statusCaps[i])); |
| 529 | } |
| 530 | |
| 531 | FrontendCapabilities caps; |
| 532 | switch (halInfo.type) { |
| 533 | case ::android::hardware::tv::tuner::V1_0::FrontendType::ANALOG: { |
| 534 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::analogCaps == |
| 535 | halInfo.frontendCaps.getDiscriminator()) { |
| 536 | FrontendAnalogCapabilities analogCaps{ |
| 537 | .typeCap = static_cast<int32_t>(halInfo.frontendCaps.analogCaps().typeCap), |
| 538 | .sifStandardCap = |
| 539 | static_cast<int32_t>(halInfo.frontendCaps.analogCaps().sifStandardCap), |
| 540 | }; |
| 541 | caps.set<FrontendCapabilities::analogCaps>(analogCaps); |
| 542 | } |
| 543 | break; |
| 544 | } |
| 545 | case ::android::hardware::tv::tuner::V1_0::FrontendType::ATSC: { |
| 546 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::atscCaps == |
| 547 | halInfo.frontendCaps.getDiscriminator()) { |
| 548 | FrontendAtscCapabilities atscCaps{ |
| 549 | .modulationCap = |
| 550 | static_cast<int32_t>(halInfo.frontendCaps.atscCaps().modulationCap), |
| 551 | }; |
| 552 | caps.set<FrontendCapabilities::atscCaps>(atscCaps); |
| 553 | } |
| 554 | break; |
| 555 | } |
| 556 | case ::android::hardware::tv::tuner::V1_0::FrontendType::ATSC3: { |
| 557 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::atsc3Caps == |
| 558 | halInfo.frontendCaps.getDiscriminator()) { |
| 559 | FrontendAtsc3Capabilities atsc3Caps{ |
| 560 | .bandwidthCap = |
| 561 | static_cast<int32_t>(halInfo.frontendCaps.atsc3Caps().bandwidthCap), |
| 562 | .modulationCap = |
| 563 | static_cast<int32_t>(halInfo.frontendCaps.atsc3Caps().modulationCap), |
| 564 | .timeInterleaveModeCap = static_cast<int32_t>( |
| 565 | halInfo.frontendCaps.atsc3Caps().timeInterleaveModeCap), |
| 566 | .codeRateCap = |
| 567 | static_cast<int32_t>(halInfo.frontendCaps.atsc3Caps().codeRateCap), |
| 568 | .demodOutputFormatCap = static_cast<int8_t>( |
| 569 | halInfo.frontendCaps.atsc3Caps().demodOutputFormatCap), |
| 570 | .fecCap = static_cast<int32_t>(halInfo.frontendCaps.atsc3Caps().fecCap), |
| 571 | }; |
| 572 | caps.set<FrontendCapabilities::atsc3Caps>(atsc3Caps); |
| 573 | } |
| 574 | break; |
| 575 | } |
| 576 | case ::android::hardware::tv::tuner::V1_0::FrontendType::DVBC: { |
| 577 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::dvbcCaps == |
| 578 | halInfo.frontendCaps.getDiscriminator()) { |
| 579 | FrontendDvbcCapabilities dvbcCaps{ |
| 580 | .modulationCap = |
| 581 | static_cast<int32_t>(halInfo.frontendCaps.dvbcCaps().modulationCap), |
| 582 | .fecCap = static_cast<int64_t>(halInfo.frontendCaps.dvbcCaps().fecCap), |
| 583 | .annexCap = static_cast<int8_t>(halInfo.frontendCaps.dvbcCaps().annexCap), |
| 584 | }; |
| 585 | caps.set<FrontendCapabilities::dvbcCaps>(dvbcCaps); |
| 586 | } |
| 587 | break; |
| 588 | } |
| 589 | case ::android::hardware::tv::tuner::V1_0::FrontendType::DVBS: { |
| 590 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::dvbsCaps == |
| 591 | halInfo.frontendCaps.getDiscriminator()) { |
| 592 | FrontendDvbsCapabilities dvbsCaps{ |
| 593 | .modulationCap = |
| 594 | static_cast<int32_t>(halInfo.frontendCaps.dvbsCaps().modulationCap), |
| 595 | .innerfecCap = |
| 596 | static_cast<int64_t>(halInfo.frontendCaps.dvbsCaps().innerfecCap), |
| 597 | .standard = static_cast<int8_t>(halInfo.frontendCaps.dvbsCaps().standard), |
| 598 | }; |
| 599 | caps.set<FrontendCapabilities::dvbsCaps>(dvbsCaps); |
| 600 | } |
| 601 | break; |
| 602 | } |
| 603 | case ::android::hardware::tv::tuner::V1_0::FrontendType::DVBT: { |
| 604 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::dvbtCaps == |
| 605 | halInfo.frontendCaps.getDiscriminator()) { |
| 606 | FrontendDvbtCapabilities dvbtCaps{ |
| 607 | .transmissionModeCap = static_cast<int32_t>( |
| 608 | halInfo.frontendCaps.dvbtCaps().transmissionModeCap), |
| 609 | .bandwidthCap = |
| 610 | static_cast<int32_t>(halInfo.frontendCaps.dvbtCaps().bandwidthCap), |
| 611 | .constellationCap = |
| 612 | static_cast<int32_t>(halInfo.frontendCaps.dvbtCaps().constellationCap), |
| 613 | .coderateCap = |
| 614 | static_cast<int32_t>(halInfo.frontendCaps.dvbtCaps().coderateCap), |
| 615 | .hierarchyCap = |
| 616 | static_cast<int32_t>(halInfo.frontendCaps.dvbtCaps().hierarchyCap), |
| 617 | .guardIntervalCap = |
| 618 | static_cast<int32_t>(halInfo.frontendCaps.dvbtCaps().guardIntervalCap), |
| 619 | .isT2Supported = halInfo.frontendCaps.dvbtCaps().isT2Supported, |
| 620 | .isMisoSupported = halInfo.frontendCaps.dvbtCaps().isMisoSupported, |
| 621 | }; |
| 622 | caps.set<FrontendCapabilities::dvbtCaps>(dvbtCaps); |
| 623 | } |
| 624 | break; |
| 625 | } |
| 626 | case ::android::hardware::tv::tuner::V1_0::FrontendType::ISDBS: { |
| 627 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::isdbsCaps == |
| 628 | halInfo.frontendCaps.getDiscriminator()) { |
| 629 | FrontendIsdbsCapabilities isdbsCaps{ |
| 630 | .modulationCap = |
| 631 | static_cast<int32_t>(halInfo.frontendCaps.isdbsCaps().modulationCap), |
| 632 | .coderateCap = |
| 633 | static_cast<int32_t>(halInfo.frontendCaps.isdbsCaps().coderateCap), |
| 634 | }; |
| 635 | caps.set<FrontendCapabilities::isdbsCaps>(isdbsCaps); |
| 636 | } |
| 637 | break; |
| 638 | } |
| 639 | case ::android::hardware::tv::tuner::V1_0::FrontendType::ISDBS3: { |
| 640 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::isdbs3Caps == |
| 641 | halInfo.frontendCaps.getDiscriminator()) { |
| 642 | FrontendIsdbs3Capabilities isdbs3Caps{ |
| 643 | .modulationCap = |
| 644 | static_cast<int32_t>(halInfo.frontendCaps.isdbs3Caps().modulationCap), |
| 645 | .coderateCap = |
| 646 | static_cast<int32_t>(halInfo.frontendCaps.isdbs3Caps().coderateCap), |
| 647 | }; |
| 648 | caps.set<FrontendCapabilities::isdbs3Caps>(isdbs3Caps); |
| 649 | } |
| 650 | break; |
| 651 | } |
| 652 | case ::android::hardware::tv::tuner::V1_0::FrontendType::ISDBT: { |
| 653 | if (HidlFrontendInfo::FrontendCapabilities::hidl_discriminator::isdbtCaps == |
| 654 | halInfo.frontendCaps.getDiscriminator()) { |
| 655 | FrontendIsdbtCapabilities isdbtCaps{ |
| 656 | .modeCap = static_cast<int32_t>(halInfo.frontendCaps.isdbtCaps().modeCap), |
| 657 | .bandwidthCap = |
| 658 | static_cast<int32_t>(halInfo.frontendCaps.isdbtCaps().bandwidthCap), |
| 659 | .modulationCap = |
| 660 | static_cast<int32_t>(halInfo.frontendCaps.isdbtCaps().modulationCap), |
| 661 | .coderateCap = |
| 662 | static_cast<int32_t>(halInfo.frontendCaps.isdbtCaps().coderateCap), |
| 663 | .guardIntervalCap = |
| 664 | static_cast<int32_t>(halInfo.frontendCaps.isdbtCaps().guardIntervalCap), |
Hongguang | 15758a8 | 2021-10-29 13:32:07 -0700 | [diff] [blame] | 665 | .timeInterleaveCap = |
| 666 | static_cast<int32_t>(FrontendIsdbtTimeInterleaveMode::UNDEFINED), |
| 667 | .isSegmentAuto = false, |
| 668 | .isFullSegment = false, |
Hongguang | 093c5f3 | 2021-08-09 19:46:34 -0700 | [diff] [blame] | 669 | }; |
| 670 | caps.set<FrontendCapabilities::isdbtCaps>(isdbtCaps); |
| 671 | } |
| 672 | break; |
| 673 | } |
| 674 | default: { |
| 675 | if (static_cast<HidlFrontendType>(info.type) == HidlFrontendType::DTMB) { |
| 676 | FrontendDtmbCapabilities dtmbCaps{ |
| 677 | .transmissionModeCap = static_cast<int32_t>(halDtmbCaps.transmissionModeCap), |
| 678 | .bandwidthCap = static_cast<int32_t>(halDtmbCaps.bandwidthCap), |
| 679 | .modulationCap = static_cast<int32_t>(halDtmbCaps.modulationCap), |
| 680 | .codeRateCap = static_cast<int32_t>(halDtmbCaps.codeRateCap), |
| 681 | .guardIntervalCap = static_cast<int32_t>(halDtmbCaps.guardIntervalCap), |
| 682 | .interleaveModeCap = static_cast<int32_t>(halDtmbCaps.interleaveModeCap), |
| 683 | }; |
| 684 | caps.set<FrontendCapabilities::dtmbCaps>(dtmbCaps); |
| 685 | } |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | info.frontendCaps = caps; |
| 691 | return info; |
| 692 | } |
| 693 | |
| 694 | } // namespace tuner |
| 695 | } // namespace tv |
| 696 | } // namespace media |
| 697 | } // namespace android |
| 698 | } // namespace aidl |