blob: 8c715a0e126542b08230bcefd5e6ab72b833ff6d [file] [log] [blame]
Hongguang4092f2f2021-07-08 18:49:12 -07001/*
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
Hongguange423acd2021-07-27 16:56:47 -070020#include <aidl/android/hardware/tv/tuner/Result.h>
Hongguang4092f2f2021-07-08 18:49:12 -070021#include <utils/Log.h>
Hongguange423acd2021-07-27 16:56:47 -070022
Hongguang4092f2f2021-07-08 18:49:12 -070023#include "Demux.h"
24#include "Descrambler.h"
25#include "Frontend.h"
26#include "Lnb.h"
Hongguange423acd2021-07-27 16:56:47 -070027#include "Tuner.h"
Hongguang4092f2f2021-07-08 18:49:12 -070028
29namespace aidl {
30namespace android {
31namespace hardware {
32namespace tv {
33namespace tuner {
34
Hongguang Chenff2c6b02021-08-07 00:12:26 +000035Tuner::Tuner() {}
36
37void Tuner::init() {
Hongguang4092f2f2021-07-08 18:49:12 -070038 // Static Frontends array to maintain local frontends information
39 // Array index matches their FrontendId in the default impl
40 mFrontendSize = 10;
Hongguang4a8ac292022-08-09 14:02:03 -070041 mFrontends[0] = ndk::SharedRefBase::make<Frontend>(FrontendType::ISDBS, 0);
42 mFrontends[1] = ndk::SharedRefBase::make<Frontend>(FrontendType::ATSC3, 1);
43 mFrontends[2] = ndk::SharedRefBase::make<Frontend>(FrontendType::DVBC, 2);
44 mFrontends[3] = ndk::SharedRefBase::make<Frontend>(FrontendType::DVBS, 3);
45 mFrontends[4] = ndk::SharedRefBase::make<Frontend>(FrontendType::DVBT, 4);
46 mFrontends[5] = ndk::SharedRefBase::make<Frontend>(FrontendType::ISDBT, 5);
47 mFrontends[6] = ndk::SharedRefBase::make<Frontend>(FrontendType::ANALOG, 6);
48 mFrontends[7] = ndk::SharedRefBase::make<Frontend>(FrontendType::ATSC, 7);
49 mFrontends[8] = ndk::SharedRefBase::make<Frontend>(FrontendType::ISDBS3, 8);
50 mFrontends[9] = ndk::SharedRefBase::make<Frontend>(FrontendType::DTMB, 9);
Hongguang4092f2f2021-07-08 18:49:12 -070051
Hongguang5766ddf2021-12-23 11:40:37 -080052 mMaxUsableFrontends[FrontendType::ISDBS] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080053 mMaxUsableFrontends[FrontendType::ATSC3] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080054 mMaxUsableFrontends[FrontendType::DVBC] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080055 mMaxUsableFrontends[FrontendType::DVBS] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080056 mMaxUsableFrontends[FrontendType::DVBT] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080057 mMaxUsableFrontends[FrontendType::ISDBT] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080058 mMaxUsableFrontends[FrontendType::ANALOG] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080059 mMaxUsableFrontends[FrontendType::ATSC] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080060 mMaxUsableFrontends[FrontendType::ISDBS3] = 1;
Hongguang5766ddf2021-12-23 11:40:37 -080061 mMaxUsableFrontends[FrontendType::DTMB] = 1;
Hongguang4092f2f2021-07-08 18:49:12 -070062
63 mLnbs.resize(2);
64 mLnbs[0] = ndk::SharedRefBase::make<Lnb>(0);
65 mLnbs[1] = ndk::SharedRefBase::make<Lnb>(1);
66}
67
68Tuner::~Tuner() {}
69
70::ndk::ScopedAStatus Tuner::getFrontendIds(std::vector<int32_t>* _aidl_return) {
71 ALOGV("%s", __FUNCTION__);
72
73 _aidl_return->resize(mFrontendSize);
74 for (int i = 0; i < mFrontendSize; i++) {
75 (*_aidl_return)[i] = mFrontends[i]->getFrontendId();
76 }
77
78 return ::ndk::ScopedAStatus::ok();
79}
80
81::ndk::ScopedAStatus Tuner::openFrontendById(int32_t in_frontendId,
82 std::shared_ptr<IFrontend>* _aidl_return) {
83 ALOGV("%s", __FUNCTION__);
84
85 if (in_frontendId >= mFrontendSize || in_frontendId < 0) {
86 ALOGW("[ WARN ] Frontend with id %d isn't available", in_frontendId);
87 *_aidl_return = nullptr;
Hongguange423acd2021-07-27 16:56:47 -070088 return ::ndk::ScopedAStatus::fromServiceSpecificError(
89 static_cast<int32_t>(Result::INVALID_ARGUMENT));
Hongguang4092f2f2021-07-08 18:49:12 -070090 }
91
Hongguang4a8ac292022-08-09 14:02:03 -070092 mFrontends[in_frontendId]->setTunerService(this->ref<Tuner>());
Hongguang4092f2f2021-07-08 18:49:12 -070093 *_aidl_return = mFrontends[in_frontendId];
94 return ::ndk::ScopedAStatus::ok();
95}
96
97::ndk::ScopedAStatus Tuner::openDemux(std::vector<int32_t>* out_demuxId,
98 std::shared_ptr<IDemux>* _aidl_return) {
99 ALOGV("%s", __FUNCTION__);
100
101 mLastUsedId += 1;
Hongguang Chenff2c6b02021-08-07 00:12:26 +0000102 mDemuxes[mLastUsedId] = ndk::SharedRefBase::make<Demux>(mLastUsedId, this->ref<Tuner>());
Hongguang4092f2f2021-07-08 18:49:12 -0700103
104 out_demuxId->push_back(mLastUsedId);
105 *_aidl_return = mDemuxes[mLastUsedId];
106
107 return ::ndk::ScopedAStatus::ok();
108}
109
110::ndk::ScopedAStatus Tuner::getDemuxCaps(DemuxCapabilities* _aidl_return) {
111 ALOGV("%s", __FUNCTION__);
112
113 // IP filter can be an MMTP filter's data source.
114 _aidl_return->linkCaps = {0x00, 0x00, 0x02, 0x00, 0x00};
115 // Support time filter testing
116 _aidl_return->bTimeFilter = true;
117
118 return ::ndk::ScopedAStatus::ok();
119}
120
121::ndk::ScopedAStatus Tuner::openDescrambler(std::shared_ptr<IDescrambler>* _aidl_return) {
122 ALOGV("%s", __FUNCTION__);
123
124 *_aidl_return = ndk::SharedRefBase::make<Descrambler>();
125
126 return ndk::ScopedAStatus::ok();
127}
128
129::ndk::ScopedAStatus Tuner::getFrontendInfo(int32_t in_frontendId, FrontendInfo* _aidl_return) {
130 ALOGV("%s", __FUNCTION__);
131
Hongguang881190f2022-01-14 13:23:37 -0800132 if (in_frontendId < 0 || in_frontendId >= mFrontendSize) {
Hongguange423acd2021-07-27 16:56:47 -0700133 return ::ndk::ScopedAStatus::fromServiceSpecificError(
134 static_cast<int32_t>(Result::INVALID_ARGUMENT));
Hongguang4092f2f2021-07-08 18:49:12 -0700135 }
136
Hongguang881190f2022-01-14 13:23:37 -0800137 mFrontends[in_frontendId]->getFrontendInfo(_aidl_return);
Hongguang4092f2f2021-07-08 18:49:12 -0700138 return ::ndk::ScopedAStatus::ok();
139}
140
141::ndk::ScopedAStatus Tuner::getLnbIds(std::vector<int32_t>* _aidl_return) {
142 ALOGV("%s", __FUNCTION__);
143
144 _aidl_return->resize(mLnbs.size());
145 for (int i = 0; i < mLnbs.size(); i++) {
146 (*_aidl_return)[i] = mLnbs[i]->getId();
147 }
148
149 return ::ndk::ScopedAStatus::ok();
150}
151
152::ndk::ScopedAStatus Tuner::openLnbById(int32_t in_lnbId, std::shared_ptr<ILnb>* _aidl_return) {
153 ALOGV("%s", __FUNCTION__);
154
155 if (in_lnbId >= mLnbs.size()) {
156 *_aidl_return = nullptr;
Hongguange423acd2021-07-27 16:56:47 -0700157 return ::ndk::ScopedAStatus::fromServiceSpecificError(
158 static_cast<int32_t>(Result::INVALID_ARGUMENT));
Hongguang4092f2f2021-07-08 18:49:12 -0700159 }
160
161 *_aidl_return = mLnbs[in_lnbId];
162 return ::ndk::ScopedAStatus::ok();
163}
164
165std::shared_ptr<Frontend> Tuner::getFrontendById(int32_t frontendId) {
166 ALOGV("%s", __FUNCTION__);
167
168 return mFrontends[frontendId];
169}
170
171::ndk::ScopedAStatus Tuner::openLnbByName(const std::string& /* in_lnbName */,
172 std::vector<int32_t>* out_lnbId,
173 std::shared_ptr<ILnb>* _aidl_return) {
174 ALOGV("%s", __FUNCTION__);
175
176 out_lnbId->push_back(1234);
177 *_aidl_return = ndk::SharedRefBase::make<Lnb>();
178
179 return ::ndk::ScopedAStatus::ok();
180}
181
Hongguangfcedda02021-12-13 17:08:02 -0800182::ndk::ScopedAStatus Tuner::setLna(bool /* in_bEnable */) {
183 ALOGV("%s", __FUNCTION__);
184
185 return ::ndk::ScopedAStatus::ok();
186}
187
Hongguang5766ddf2021-12-23 11:40:37 -0800188::ndk::ScopedAStatus Tuner::setMaxNumberOfFrontends(FrontendType in_frontendType,
189 int32_t in_maxNumber) {
190 ALOGV("%s", __FUNCTION__);
191
192 // In the default implementation, every type only has one frontend.
193 if (in_maxNumber < 0 || in_maxNumber > 1) {
194 return ::ndk::ScopedAStatus::fromServiceSpecificError(
195 static_cast<int32_t>(Result::INVALID_ARGUMENT));
196 }
197 mMaxUsableFrontends[in_frontendType] = in_maxNumber;
198 return ::ndk::ScopedAStatus::ok();
199}
200
201::ndk::ScopedAStatus Tuner::getMaxNumberOfFrontends(FrontendType in_frontendType,
202 int32_t* _aidl_return) {
203 *_aidl_return = mMaxUsableFrontends[in_frontendType];
204 return ::ndk::ScopedAStatus::ok();
205}
206
Ray Chin8fe32b22022-11-01 11:51:23 +0800207::ndk::ScopedAStatus Tuner::isLnaSupported(bool* _aidl_return) {
208 ALOGV("%s", __FUNCTION__);
209
210 *_aidl_return = true;
211 return ::ndk::ScopedAStatus::ok();
212}
213
Hongguang2ecfc392021-11-23 10:29:15 -0800214binder_status_t Tuner::dump(int fd, const char** args, uint32_t numArgs) {
215 ALOGV("%s", __FUNCTION__);
216 {
217 dprintf(fd, "Frontends:\n");
218 for (int i = 0; i < mFrontendSize; i++) {
219 mFrontends[i]->dump(fd, args, numArgs);
Hongguang2ecfc392021-11-23 10:29:15 -0800220 }
221 }
222 {
223 dprintf(fd, "Demuxs:\n");
224 map<int32_t, std::shared_ptr<Demux>>::iterator it;
225 for (it = mDemuxes.begin(); it != mDemuxes.end(); it++) {
226 it->second->dump(fd, args, numArgs);
227 }
228 }
229 {
230 dprintf(fd, "Lnbs:\n");
231 for (int i = 0; i < mLnbs.size(); i++) {
232 mLnbs[i]->dump(fd, args, numArgs);
233 }
234 }
235 return STATUS_OK;
236}
237
Hongguang4092f2f2021-07-08 18:49:12 -0700238void Tuner::setFrontendAsDemuxSource(int32_t frontendId, int32_t demuxId) {
239 mFrontendToDemux[frontendId] = demuxId;
240 if (mFrontends[frontendId] != nullptr && mFrontends[frontendId]->isLocked()) {
241 mDemuxes[demuxId]->startFrontendInputLoop();
242 }
243}
244
245void Tuner::removeDemux(int32_t demuxId) {
246 map<int32_t, int32_t>::iterator it;
247 for (it = mFrontendToDemux.begin(); it != mFrontendToDemux.end(); it++) {
248 if (it->second == demuxId) {
249 it = mFrontendToDemux.erase(it);
250 break;
251 }
252 }
253 mDemuxes.erase(demuxId);
254}
255
256void Tuner::removeFrontend(int32_t frontendId) {
Hongguang2ecfc392021-11-23 10:29:15 -0800257 map<int32_t, int32_t>::iterator it = mFrontendToDemux.find(frontendId);
258 if (it != mFrontendToDemux.end()) {
259 mDemuxes.erase(it->second);
260 }
Hongguang4092f2f2021-07-08 18:49:12 -0700261 mFrontendToDemux.erase(frontendId);
262}
263
264void Tuner::frontendStopTune(int32_t frontendId) {
265 map<int32_t, int32_t>::iterator it = mFrontendToDemux.find(frontendId);
266 int32_t demuxId;
267 if (it != mFrontendToDemux.end()) {
268 demuxId = it->second;
269 mDemuxes[demuxId]->stopFrontendInput();
270 }
271}
272
273void Tuner::frontendStartTune(int32_t frontendId) {
274 map<int32_t, int32_t>::iterator it = mFrontendToDemux.find(frontendId);
275 int32_t demuxId;
276 if (it != mFrontendToDemux.end()) {
277 demuxId = it->second;
278 mDemuxes[demuxId]->startFrontendInputLoop();
279 }
280}
281
282} // namespace tuner
283} // namespace tv
284} // namespace hardware
285} // namespace android
286} // namespace aidl