Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 "android.hardware.tv.tuner-service.example-Tuner" |
| 19 | |
Hongguang | e423acd | 2021-07-27 16:56:47 -0700 | [diff] [blame] | 20 | #include <aidl/android/hardware/tv/tuner/Result.h> |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
Hongguang | e423acd | 2021-07-27 16:56:47 -0700 | [diff] [blame] | 22 | |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 23 | #include "Demux.h" |
| 24 | #include "Descrambler.h" |
| 25 | #include "Frontend.h" |
| 26 | #include "Lnb.h" |
Hongguang | e423acd | 2021-07-27 16:56:47 -0700 | [diff] [blame] | 27 | #include "Tuner.h" |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 28 | |
| 29 | namespace aidl { |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace tv { |
| 33 | namespace tuner { |
| 34 | |
Hongguang Chen | ff2c6b0 | 2021-08-07 00:12:26 +0000 | [diff] [blame^] | 35 | Tuner::Tuner() {} |
| 36 | |
| 37 | void Tuner::init() { |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 38 | // Static Frontends array to maintain local frontends information |
| 39 | // Array index matches their FrontendId in the default impl |
| 40 | mFrontendSize = 10; |
Hongguang Chen | ff2c6b0 | 2021-08-07 00:12:26 +0000 | [diff] [blame^] | 41 | mFrontends[0] = ndk::SharedRefBase::make<Frontend>(FrontendType::ISDBS, 0, this->ref<Tuner>()); |
| 42 | mFrontends[1] = ndk::SharedRefBase::make<Frontend>(FrontendType::ATSC3, 1, this->ref<Tuner>()); |
| 43 | mFrontends[2] = ndk::SharedRefBase::make<Frontend>(FrontendType::DVBC, 2, this->ref<Tuner>()); |
| 44 | mFrontends[3] = ndk::SharedRefBase::make<Frontend>(FrontendType::DVBS, 3, this->ref<Tuner>()); |
| 45 | mFrontends[4] = ndk::SharedRefBase::make<Frontend>(FrontendType::DVBT, 4, this->ref<Tuner>()); |
| 46 | mFrontends[5] = ndk::SharedRefBase::make<Frontend>(FrontendType::ISDBT, 5, this->ref<Tuner>()); |
| 47 | mFrontends[6] = ndk::SharedRefBase::make<Frontend>(FrontendType::ANALOG, 6, this->ref<Tuner>()); |
| 48 | mFrontends[7] = ndk::SharedRefBase::make<Frontend>(FrontendType::ATSC, 7, this->ref<Tuner>()); |
| 49 | mFrontends[8] = ndk::SharedRefBase::make<Frontend>(FrontendType::ISDBS3, 8, this->ref<Tuner>()); |
| 50 | mFrontends[9] = ndk::SharedRefBase::make<Frontend>(FrontendType::DTMB, 9, this->ref<Tuner>()); |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 51 | |
| 52 | vector<FrontendStatusType> statusCaps; |
| 53 | |
| 54 | FrontendCapabilities capsIsdbs; |
| 55 | capsIsdbs.set<FrontendCapabilities::Tag::isdbsCaps>(FrontendIsdbsCapabilities()); |
| 56 | mFrontendCaps[0] = capsIsdbs; |
| 57 | statusCaps = { |
| 58 | FrontendStatusType::DEMOD_LOCK, FrontendStatusType::SNR, |
| 59 | FrontendStatusType::FEC, FrontendStatusType::MODULATION, |
| 60 | FrontendStatusType::MODULATIONS, FrontendStatusType::ROLL_OFF, |
| 61 | }; |
| 62 | mFrontendStatusCaps[0] = statusCaps; |
| 63 | |
| 64 | FrontendCapabilities capsAtsc3; |
| 65 | capsAtsc3.set<FrontendCapabilities::Tag::atsc3Caps>(FrontendAtsc3Capabilities()); |
| 66 | mFrontendCaps[1] = capsAtsc3; |
| 67 | statusCaps = { |
| 68 | FrontendStatusType::BER, |
| 69 | FrontendStatusType::PER, |
| 70 | FrontendStatusType::ATSC3_PLP_INFO, |
| 71 | FrontendStatusType::MODULATIONS, |
| 72 | FrontendStatusType::BERS, |
| 73 | FrontendStatusType::INTERLEAVINGS, |
| 74 | FrontendStatusType::BANDWIDTH, |
| 75 | }; |
| 76 | mFrontendStatusCaps[1] = statusCaps; |
| 77 | |
| 78 | FrontendCapabilities capsDvbc; |
| 79 | capsDvbc.set<FrontendCapabilities::Tag::dvbcCaps>(FrontendDvbcCapabilities()); |
| 80 | mFrontendCaps[2] = capsDvbc; |
| 81 | statusCaps = { |
| 82 | FrontendStatusType::PRE_BER, FrontendStatusType::SIGNAL_QUALITY, |
| 83 | FrontendStatusType::MODULATION, FrontendStatusType::SPECTRAL, |
| 84 | FrontendStatusType::MODULATIONS, FrontendStatusType::CODERATES, |
| 85 | FrontendStatusType::INTERLEAVINGS, FrontendStatusType::BANDWIDTH, |
| 86 | }; |
| 87 | mFrontendStatusCaps[2] = statusCaps; |
| 88 | |
| 89 | FrontendCapabilities capsDvbs; |
| 90 | capsDvbs.set<FrontendCapabilities::Tag::dvbsCaps>(FrontendDvbsCapabilities()); |
| 91 | mFrontendCaps[3] = capsDvbs; |
| 92 | statusCaps = { |
| 93 | FrontendStatusType::SIGNAL_STRENGTH, FrontendStatusType::SYMBOL_RATE, |
| 94 | FrontendStatusType::MODULATION, FrontendStatusType::MODULATIONS, |
| 95 | FrontendStatusType::ROLL_OFF, FrontendStatusType::IS_MISO, |
| 96 | }; |
| 97 | mFrontendStatusCaps[3] = statusCaps; |
| 98 | |
| 99 | FrontendCapabilities capsDvbt; |
| 100 | capsDvbt.set<FrontendCapabilities::Tag::dvbtCaps>(FrontendDvbtCapabilities()); |
| 101 | mFrontendCaps[4] = capsDvbt; |
| 102 | statusCaps = { |
| 103 | FrontendStatusType::EWBS, |
| 104 | FrontendStatusType::PLP_ID, |
| 105 | FrontendStatusType::HIERARCHY, |
| 106 | FrontendStatusType::MODULATIONS, |
| 107 | FrontendStatusType::BANDWIDTH, |
| 108 | FrontendStatusType::GUARD_INTERVAL, |
| 109 | FrontendStatusType::TRANSMISSION_MODE, |
| 110 | FrontendStatusType::T2_SYSTEM_ID, |
| 111 | }; |
| 112 | mFrontendStatusCaps[4] = statusCaps; |
| 113 | |
| 114 | FrontendCapabilities capsIsdbt; |
| 115 | FrontendIsdbtCapabilities isdbtCaps{ |
| 116 | .modeCap = (int)FrontendIsdbtMode::MODE_1 | (int)FrontendIsdbtMode::MODE_2, |
| 117 | .bandwidthCap = (int)FrontendIsdbtBandwidth::BANDWIDTH_6MHZ, |
| 118 | .modulationCap = (int)FrontendIsdbtModulation::MOD_16QAM, |
Hongguang | e69a3b2 | 2021-08-03 14:23:42 -0700 | [diff] [blame] | 119 | .coderateCap = (int)FrontendIsdbtCoderate::CODERATE_4_5 | |
| 120 | (int)FrontendIsdbtCoderate::CODERATE_6_7, |
| 121 | .guardIntervalCap = (int)FrontendIsdbtGuardInterval::INTERVAL_1_128, |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 122 | }; |
| 123 | capsIsdbt.set<FrontendCapabilities::Tag::isdbtCaps>(isdbtCaps); |
| 124 | mFrontendCaps[5] = capsIsdbt; |
| 125 | statusCaps = { |
| 126 | FrontendStatusType::AGC, |
| 127 | FrontendStatusType::LNA, |
| 128 | FrontendStatusType::MODULATION, |
| 129 | FrontendStatusType::MODULATIONS, |
| 130 | FrontendStatusType::BANDWIDTH, |
| 131 | FrontendStatusType::GUARD_INTERVAL, |
| 132 | FrontendStatusType::TRANSMISSION_MODE, |
| 133 | FrontendStatusType::ISDBT_SEGMENTS, |
| 134 | }; |
| 135 | mFrontendStatusCaps[5] = statusCaps; |
| 136 | |
| 137 | FrontendCapabilities capsAnalog; |
| 138 | capsAnalog.set<FrontendCapabilities::Tag::analogCaps>(FrontendAnalogCapabilities()); |
| 139 | mFrontendCaps[6] = capsAnalog; |
| 140 | statusCaps = { |
| 141 | FrontendStatusType::LAYER_ERROR, |
| 142 | FrontendStatusType::MER, |
| 143 | FrontendStatusType::UEC, |
| 144 | FrontendStatusType::TS_DATA_RATES, |
| 145 | }; |
| 146 | mFrontendStatusCaps[6] = statusCaps; |
| 147 | |
| 148 | FrontendCapabilities capsAtsc; |
| 149 | capsAtsc.set<FrontendCapabilities::Tag::atscCaps>(FrontendAtscCapabilities()); |
| 150 | mFrontendCaps[7] = capsAtsc; |
| 151 | statusCaps = { |
| 152 | FrontendStatusType::FREQ_OFFSET, |
| 153 | FrontendStatusType::RF_LOCK, |
| 154 | FrontendStatusType::MODULATIONS, |
| 155 | FrontendStatusType::IS_LINEAR, |
| 156 | }; |
| 157 | mFrontendStatusCaps[7] = statusCaps; |
| 158 | |
| 159 | FrontendCapabilities capsIsdbs3; |
| 160 | capsIsdbs3.set<FrontendCapabilities::Tag::isdbs3Caps>(FrontendIsdbs3Capabilities()); |
| 161 | mFrontendCaps[8] = capsIsdbs3; |
| 162 | statusCaps = { |
| 163 | FrontendStatusType::DEMOD_LOCK, FrontendStatusType::MODULATION, |
| 164 | FrontendStatusType::MODULATIONS, FrontendStatusType::ROLL_OFF, |
| 165 | FrontendStatusType::IS_SHORT_FRAMES, |
| 166 | }; |
| 167 | mFrontendStatusCaps[8] = statusCaps; |
| 168 | |
| 169 | FrontendCapabilities capsDtmb; |
| 170 | capsDtmb.set<FrontendCapabilities::Tag::dtmbCaps>(FrontendDtmbCapabilities()); |
| 171 | mFrontendCaps[9] = capsDtmb; |
| 172 | statusCaps = { |
| 173 | FrontendStatusType::MODULATIONS, FrontendStatusType::INTERLEAVINGS, |
| 174 | FrontendStatusType::BANDWIDTH, FrontendStatusType::GUARD_INTERVAL, |
| 175 | FrontendStatusType::TRANSMISSION_MODE, |
| 176 | }; |
| 177 | mFrontendStatusCaps[9] = statusCaps; |
| 178 | |
| 179 | mLnbs.resize(2); |
| 180 | mLnbs[0] = ndk::SharedRefBase::make<Lnb>(0); |
| 181 | mLnbs[1] = ndk::SharedRefBase::make<Lnb>(1); |
| 182 | } |
| 183 | |
| 184 | Tuner::~Tuner() {} |
| 185 | |
| 186 | ::ndk::ScopedAStatus Tuner::getFrontendIds(std::vector<int32_t>* _aidl_return) { |
| 187 | ALOGV("%s", __FUNCTION__); |
| 188 | |
| 189 | _aidl_return->resize(mFrontendSize); |
| 190 | for (int i = 0; i < mFrontendSize; i++) { |
| 191 | (*_aidl_return)[i] = mFrontends[i]->getFrontendId(); |
| 192 | } |
| 193 | |
| 194 | return ::ndk::ScopedAStatus::ok(); |
| 195 | } |
| 196 | |
| 197 | ::ndk::ScopedAStatus Tuner::openFrontendById(int32_t in_frontendId, |
| 198 | std::shared_ptr<IFrontend>* _aidl_return) { |
| 199 | ALOGV("%s", __FUNCTION__); |
| 200 | |
| 201 | if (in_frontendId >= mFrontendSize || in_frontendId < 0) { |
| 202 | ALOGW("[ WARN ] Frontend with id %d isn't available", in_frontendId); |
| 203 | *_aidl_return = nullptr; |
Hongguang | e423acd | 2021-07-27 16:56:47 -0700 | [diff] [blame] | 204 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 205 | static_cast<int32_t>(Result::INVALID_ARGUMENT)); |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | *_aidl_return = mFrontends[in_frontendId]; |
| 209 | return ::ndk::ScopedAStatus::ok(); |
| 210 | } |
| 211 | |
| 212 | ::ndk::ScopedAStatus Tuner::openDemux(std::vector<int32_t>* out_demuxId, |
| 213 | std::shared_ptr<IDemux>* _aidl_return) { |
| 214 | ALOGV("%s", __FUNCTION__); |
| 215 | |
| 216 | mLastUsedId += 1; |
Hongguang Chen | ff2c6b0 | 2021-08-07 00:12:26 +0000 | [diff] [blame^] | 217 | mDemuxes[mLastUsedId] = ndk::SharedRefBase::make<Demux>(mLastUsedId, this->ref<Tuner>()); |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 218 | |
| 219 | out_demuxId->push_back(mLastUsedId); |
| 220 | *_aidl_return = mDemuxes[mLastUsedId]; |
| 221 | |
| 222 | return ::ndk::ScopedAStatus::ok(); |
| 223 | } |
| 224 | |
| 225 | ::ndk::ScopedAStatus Tuner::getDemuxCaps(DemuxCapabilities* _aidl_return) { |
| 226 | ALOGV("%s", __FUNCTION__); |
| 227 | |
| 228 | // IP filter can be an MMTP filter's data source. |
| 229 | _aidl_return->linkCaps = {0x00, 0x00, 0x02, 0x00, 0x00}; |
| 230 | // Support time filter testing |
| 231 | _aidl_return->bTimeFilter = true; |
| 232 | |
| 233 | return ::ndk::ScopedAStatus::ok(); |
| 234 | } |
| 235 | |
| 236 | ::ndk::ScopedAStatus Tuner::openDescrambler(std::shared_ptr<IDescrambler>* _aidl_return) { |
| 237 | ALOGV("%s", __FUNCTION__); |
| 238 | |
| 239 | *_aidl_return = ndk::SharedRefBase::make<Descrambler>(); |
| 240 | |
| 241 | return ndk::ScopedAStatus::ok(); |
| 242 | } |
| 243 | |
| 244 | ::ndk::ScopedAStatus Tuner::getFrontendInfo(int32_t in_frontendId, FrontendInfo* _aidl_return) { |
| 245 | ALOGV("%s", __FUNCTION__); |
| 246 | |
| 247 | if (in_frontendId >= mFrontendSize) { |
Hongguang | e423acd | 2021-07-27 16:56:47 -0700 | [diff] [blame] | 248 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 249 | static_cast<int32_t>(Result::INVALID_ARGUMENT)); |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // assign randomly selected values for testing. |
| 253 | *_aidl_return = { |
| 254 | .type = mFrontends[in_frontendId]->getFrontendType(), |
| 255 | .minFrequency = 139, |
| 256 | .maxFrequency = 1139, |
| 257 | .minSymbolRate = 45, |
| 258 | .maxSymbolRate = 1145, |
| 259 | .acquireRange = 30, |
| 260 | .exclusiveGroupId = 57, |
| 261 | .statusCaps = mFrontendStatusCaps[in_frontendId], |
| 262 | .frontendCaps = mFrontendCaps[in_frontendId], |
| 263 | }; |
| 264 | |
| 265 | return ::ndk::ScopedAStatus::ok(); |
| 266 | } |
| 267 | |
| 268 | ::ndk::ScopedAStatus Tuner::getLnbIds(std::vector<int32_t>* _aidl_return) { |
| 269 | ALOGV("%s", __FUNCTION__); |
| 270 | |
| 271 | _aidl_return->resize(mLnbs.size()); |
| 272 | for (int i = 0; i < mLnbs.size(); i++) { |
| 273 | (*_aidl_return)[i] = mLnbs[i]->getId(); |
| 274 | } |
| 275 | |
| 276 | return ::ndk::ScopedAStatus::ok(); |
| 277 | } |
| 278 | |
| 279 | ::ndk::ScopedAStatus Tuner::openLnbById(int32_t in_lnbId, std::shared_ptr<ILnb>* _aidl_return) { |
| 280 | ALOGV("%s", __FUNCTION__); |
| 281 | |
| 282 | if (in_lnbId >= mLnbs.size()) { |
| 283 | *_aidl_return = nullptr; |
Hongguang | e423acd | 2021-07-27 16:56:47 -0700 | [diff] [blame] | 284 | return ::ndk::ScopedAStatus::fromServiceSpecificError( |
| 285 | static_cast<int32_t>(Result::INVALID_ARGUMENT)); |
Hongguang | 4092f2f | 2021-07-08 18:49:12 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | *_aidl_return = mLnbs[in_lnbId]; |
| 289 | return ::ndk::ScopedAStatus::ok(); |
| 290 | } |
| 291 | |
| 292 | std::shared_ptr<Frontend> Tuner::getFrontendById(int32_t frontendId) { |
| 293 | ALOGV("%s", __FUNCTION__); |
| 294 | |
| 295 | return mFrontends[frontendId]; |
| 296 | } |
| 297 | |
| 298 | ::ndk::ScopedAStatus Tuner::openLnbByName(const std::string& /* in_lnbName */, |
| 299 | std::vector<int32_t>* out_lnbId, |
| 300 | std::shared_ptr<ILnb>* _aidl_return) { |
| 301 | ALOGV("%s", __FUNCTION__); |
| 302 | |
| 303 | out_lnbId->push_back(1234); |
| 304 | *_aidl_return = ndk::SharedRefBase::make<Lnb>(); |
| 305 | |
| 306 | return ::ndk::ScopedAStatus::ok(); |
| 307 | } |
| 308 | |
| 309 | void Tuner::setFrontendAsDemuxSource(int32_t frontendId, int32_t demuxId) { |
| 310 | mFrontendToDemux[frontendId] = demuxId; |
| 311 | if (mFrontends[frontendId] != nullptr && mFrontends[frontendId]->isLocked()) { |
| 312 | mDemuxes[demuxId]->startFrontendInputLoop(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void Tuner::removeDemux(int32_t demuxId) { |
| 317 | map<int32_t, int32_t>::iterator it; |
| 318 | for (it = mFrontendToDemux.begin(); it != mFrontendToDemux.end(); it++) { |
| 319 | if (it->second == demuxId) { |
| 320 | it = mFrontendToDemux.erase(it); |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | mDemuxes.erase(demuxId); |
| 325 | } |
| 326 | |
| 327 | void Tuner::removeFrontend(int32_t frontendId) { |
| 328 | mFrontendToDemux.erase(frontendId); |
| 329 | } |
| 330 | |
| 331 | void Tuner::frontendStopTune(int32_t frontendId) { |
| 332 | map<int32_t, int32_t>::iterator it = mFrontendToDemux.find(frontendId); |
| 333 | int32_t demuxId; |
| 334 | if (it != mFrontendToDemux.end()) { |
| 335 | demuxId = it->second; |
| 336 | mDemuxes[demuxId]->stopFrontendInput(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void Tuner::frontendStartTune(int32_t frontendId) { |
| 341 | map<int32_t, int32_t>::iterator it = mFrontendToDemux.find(frontendId); |
| 342 | int32_t demuxId; |
| 343 | if (it != mFrontendToDemux.end()) { |
| 344 | demuxId = it->second; |
| 345 | mDemuxes[demuxId]->startFrontendInputLoop(); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | } // namespace tuner |
| 350 | } // namespace tv |
| 351 | } // namespace hardware |
| 352 | } // namespace android |
| 353 | } // namespace aidl |