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