blob: c3dcd1d8e45ea3b703a8f8516dad2b4815308f3d [file] [log] [blame]
Amy Zhangbb94eeb2020-07-09 22:48:04 -07001/*
2 * Copyright 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 "android.hardware.tv.tuner@1.1-Tuner"
18
19#include "Tuner.h"
20#include <utils/Log.h>
21#include "Demux.h"
22#include "Descrambler.h"
23#include "Frontend.h"
24#include "Lnb.h"
25
26namespace android {
27namespace hardware {
28namespace tv {
29namespace tuner {
30namespace V1_0 {
31namespace implementation {
32
33Tuner::Tuner() {
34 // Static Frontends array to maintain local frontends information
35 // Array index matches their FrontendId in the default impl
Amy Zhang4c49c152020-08-25 21:08:19 -070036 mFrontendSize = 9;
Amy Zhangbb94eeb2020-07-09 22:48:04 -070037 mFrontends[0] = new Frontend(FrontendType::DVBT, 0, this);
38 mFrontends[1] = new Frontend(FrontendType::ATSC, 1, this);
39 mFrontends[2] = new Frontend(FrontendType::DVBC, 2, this);
40 mFrontends[3] = new Frontend(FrontendType::DVBS, 3, this);
41 mFrontends[4] = new Frontend(FrontendType::DVBT, 4, this);
42 mFrontends[5] = new Frontend(FrontendType::ISDBT, 5, this);
43 mFrontends[6] = new Frontend(FrontendType::ANALOG, 6, this);
44 mFrontends[7] = new Frontend(FrontendType::ATSC, 7, this);
Amy Zhang4c49c152020-08-25 21:08:19 -070045 mFrontends[8] =
46 new Frontend(static_cast<V1_0::FrontendType>(V1_1::FrontendType::DTMB), 8, this);
Amy Zhangbb94eeb2020-07-09 22:48:04 -070047
48 FrontendInfo::FrontendCapabilities caps;
Amy Zhangbb94eeb2020-07-09 22:48:04 -070049 caps = FrontendInfo::FrontendCapabilities();
50 caps.dvbtCaps(FrontendDvbtCapabilities());
51 mFrontendCaps[0] = caps;
52
53 caps = FrontendInfo::FrontendCapabilities();
54 caps.atscCaps(FrontendAtscCapabilities());
55 mFrontendCaps[1] = caps;
56
57 caps = FrontendInfo::FrontendCapabilities();
58 caps.dvbcCaps(FrontendDvbcCapabilities());
59 mFrontendCaps[2] = caps;
60
61 caps = FrontendInfo::FrontendCapabilities();
62 caps.dvbsCaps(FrontendDvbsCapabilities());
63 mFrontendCaps[3] = caps;
64
65 caps = FrontendInfo::FrontendCapabilities();
66 caps.dvbtCaps(FrontendDvbtCapabilities());
67 mFrontendCaps[4] = caps;
68
69 caps = FrontendInfo::FrontendCapabilities();
70 FrontendIsdbtCapabilities isdbtCaps{
71 .modeCap = FrontendIsdbtMode::MODE_1 | FrontendIsdbtMode::MODE_2,
72 .bandwidthCap = (unsigned int)FrontendIsdbtBandwidth::BANDWIDTH_6MHZ,
73 .modulationCap = (unsigned int)FrontendIsdbtModulation::MOD_16QAM,
74 // ISDBT shares coderate and guard interval with DVBT
75 .coderateCap = FrontendDvbtCoderate::CODERATE_4_5 | FrontendDvbtCoderate::CODERATE_6_7,
76 .guardIntervalCap = (unsigned int)FrontendDvbtGuardInterval::INTERVAL_1_128,
77 };
78 caps.isdbtCaps(isdbtCaps);
79 mFrontendCaps[5] = caps;
80
81 caps = FrontendInfo::FrontendCapabilities();
82 caps.analogCaps(FrontendAnalogCapabilities());
83 mFrontendCaps[6] = caps;
84
85 caps = FrontendInfo::FrontendCapabilities();
86 caps.atscCaps(FrontendAtscCapabilities());
87 mFrontendCaps[7] = caps;
88
89 mLnbs.resize(2);
90 mLnbs[0] = new Lnb(0);
91 mLnbs[1] = new Lnb(1);
92}
93
94Tuner::~Tuner() {}
95
96Return<void> Tuner::getFrontendIds(getFrontendIds_cb _hidl_cb) {
97 ALOGV("%s", __FUNCTION__);
98
99 vector<FrontendId> frontendIds;
100 frontendIds.resize(mFrontendSize);
101 for (int i = 0; i < mFrontendSize; i++) {
102 frontendIds[i] = mFrontends[i]->getFrontendId();
103 }
104
105 _hidl_cb(Result::SUCCESS, frontendIds);
106 return Void();
107}
108
109Return<void> Tuner::openFrontendById(uint32_t frontendId, openFrontendById_cb _hidl_cb) {
110 ALOGV("%s", __FUNCTION__);
111
112 if (frontendId >= mFrontendSize || frontendId < 0) {
113 ALOGW("[ WARN ] Frontend with id %d isn't available", frontendId);
114 _hidl_cb(Result::UNAVAILABLE, nullptr);
115 return Void();
116 }
117
118 _hidl_cb(Result::SUCCESS, mFrontends[frontendId]);
119 return Void();
120}
121
122Return<void> Tuner::openDemux(openDemux_cb _hidl_cb) {
123 ALOGV("%s", __FUNCTION__);
124
125 uint32_t demuxId = mLastUsedId + 1;
126 mLastUsedId += 1;
127 sp<Demux> demux = new Demux(demuxId, this);
128 mDemuxes[demuxId] = demux;
129
130 _hidl_cb(Result::SUCCESS, demuxId, demux);
131 return Void();
132}
133
134Return<void> Tuner::getDemuxCaps(getDemuxCaps_cb _hidl_cb) {
135 ALOGV("%s", __FUNCTION__);
136
137 DemuxCapabilities caps;
138
139 // IP filter can be an MMTP filter's data source.
140 caps.linkCaps = {0x00, 0x00, 0x02, 0x00, 0x00};
141 _hidl_cb(Result::SUCCESS, caps);
142 return Void();
143}
144
145Return<void> Tuner::openDescrambler(openDescrambler_cb _hidl_cb) {
146 ALOGV("%s", __FUNCTION__);
147
148 sp<V1_0::IDescrambler> descrambler = new Descrambler();
149
150 _hidl_cb(Result::SUCCESS, descrambler);
151 return Void();
152}
153
154Return<void> Tuner::getFrontendInfo(FrontendId frontendId, getFrontendInfo_cb _hidl_cb) {
155 ALOGV("%s", __FUNCTION__);
156
157 FrontendInfo info;
158 if (frontendId >= mFrontendSize) {
159 _hidl_cb(Result::INVALID_ARGUMENT, info);
160 return Void();
161 }
162
163 vector<FrontendStatusType> statusCaps = {
164 FrontendStatusType::DEMOD_LOCK,
165 FrontendStatusType::SNR,
166 FrontendStatusType::FEC,
167 FrontendStatusType::MODULATION,
168 FrontendStatusType::PLP_ID,
169 FrontendStatusType::LAYER_ERROR,
170 FrontendStatusType::ATSC3_PLP_INFO,
171 };
172 // assign randomly selected values for testing.
173 info = {
174 .type = mFrontends[frontendId]->getFrontendType(),
175 .minFrequency = 139,
176 .maxFrequency = 1139,
177 .minSymbolRate = 45,
178 .maxSymbolRate = 1145,
179 .acquireRange = 30,
180 .exclusiveGroupId = 57,
181 .statusCaps = statusCaps,
182 .frontendCaps = mFrontendCaps[frontendId],
183 };
184
185 _hidl_cb(Result::SUCCESS, info);
186 return Void();
187}
188
189Return<void> Tuner::getLnbIds(getLnbIds_cb _hidl_cb) {
190 ALOGV("%s", __FUNCTION__);
191
192 vector<V1_0::LnbId> lnbIds;
193 lnbIds.resize(mLnbs.size());
194 for (int i = 0; i < lnbIds.size(); i++) {
195 lnbIds[i] = mLnbs[i]->getId();
196 }
197
198 _hidl_cb(Result::SUCCESS, lnbIds);
199 return Void();
200}
201
202Return<void> Tuner::openLnbById(V1_0::LnbId lnbId, openLnbById_cb _hidl_cb) {
203 ALOGV("%s", __FUNCTION__);
204
205 if (lnbId >= mLnbs.size()) {
206 _hidl_cb(Result::INVALID_ARGUMENT, nullptr);
207 return Void();
208 }
209
210 _hidl_cb(Result::SUCCESS, mLnbs[lnbId]);
211 return Void();
212}
213
214sp<Frontend> Tuner::getFrontendById(uint32_t frontendId) {
215 ALOGV("%s", __FUNCTION__);
216
217 return mFrontends[frontendId];
218}
219
220Return<void> Tuner::openLnbByName(const hidl_string& /*lnbName*/, openLnbByName_cb _hidl_cb) {
221 ALOGV("%s", __FUNCTION__);
222
223 sp<V1_0::ILnb> lnb = new Lnb();
224
225 _hidl_cb(Result::SUCCESS, 1234, lnb);
226 return Void();
227}
228
Amy Zhang4c49c152020-08-25 21:08:19 -0700229Return<void> Tuner::getFrontendDtmbCapabilities(uint32_t frontendId,
230 getFrontendDtmbCapabilities_cb _hidl_cb) {
231 ALOGV("%s", __FUNCTION__);
232
233 if (mFrontends[frontendId] != nullptr &&
234 (mFrontends[frontendId]->getFrontendType() ==
235 static_cast<V1_0::FrontendType>(V1_1::FrontendType::DTMB))) {
236 _hidl_cb(Result::SUCCESS, mDtmbCaps);
237 } else {
238 _hidl_cb(Result::UNAVAILABLE, mDtmbCaps);
239 }
240 return Void();
241}
242
Amy Zhangbb94eeb2020-07-09 22:48:04 -0700243void Tuner::setFrontendAsDemuxSource(uint32_t frontendId, uint32_t demuxId) {
244 mFrontendToDemux[frontendId] = demuxId;
245 if (mFrontends[frontendId] != nullptr && mFrontends[frontendId]->isLocked()) {
246 mDemuxes[demuxId]->startFrontendInputLoop();
247 }
248}
249
Amy Zhang80cb9602020-07-15 13:06:39 -0700250void Tuner::removeDemux(uint32_t demuxId) {
251 map<uint32_t, uint32_t>::iterator it;
Amy Zhang68afca62020-07-20 18:28:58 -0700252 for (it = mFrontendToDemux.begin(); it != mFrontendToDemux.end(); it++) {
Amy Zhang80cb9602020-07-15 13:06:39 -0700253 if (it->second == demuxId) {
254 it = mFrontendToDemux.erase(it);
Amy Zhang68afca62020-07-20 18:28:58 -0700255 break;
Amy Zhang80cb9602020-07-15 13:06:39 -0700256 }
257 }
258 mDemuxes.erase(demuxId);
259}
260
261void Tuner::removeFrontend(uint32_t frontendId) {
262 mFrontendToDemux.erase(frontendId);
263}
264
Amy Zhangbb94eeb2020-07-09 22:48:04 -0700265void Tuner::frontendStopTune(uint32_t frontendId) {
266 map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
267 uint32_t demuxId;
268 if (it != mFrontendToDemux.end()) {
269 demuxId = it->second;
270 mDemuxes[demuxId]->stopFrontendInput();
271 }
272}
273
274void Tuner::frontendStartTune(uint32_t frontendId) {
275 map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
276 uint32_t demuxId;
277 if (it != mFrontendToDemux.end()) {
278 demuxId = it->second;
279 mDemuxes[demuxId]->startFrontendInputLoop();
280 }
281}
282
283} // namespace implementation
284} // namespace V1_0
285} // namespace tuner
286} // namespace tv
287} // namespace hardware
288} // namespace android