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_TAG "TunerClient"
18
19 #include <android/binder_manager.h>
20 #include <android-base/logging.h>
21 #include <utils/Log.h>
22
23 #include "TunerClient.h"
24
25 using ::aidl::android::hardware::tv::tuner::FrontendStatusType;
26 using ::aidl::android::hardware::tv::tuner::FrontendType;
27
28 namespace android {
29
30 shared_ptr<ITunerService> TunerClient::mTunerService;
31 int32_t TunerClient::mTunerVersion;
32
33 /////////////// TunerClient ///////////////////////
34
TunerClient()35 TunerClient::TunerClient() {
36 if (mTunerService == nullptr) {
37 ::ndk::SpAIBinder binder(AServiceManager_getService("media.tuner"));
38 mTunerService = ITunerService::fromBinder(binder);
39 }
40 if (mTunerService == nullptr) {
41 ALOGE("Failed to get tuner service");
42 } else {
43 mTunerService->getTunerHalVersion(&mTunerVersion);
44 }
45 }
46
~TunerClient()47 TunerClient::~TunerClient() {
48 }
49
getFrontendIds()50 vector<int32_t> TunerClient::getFrontendIds() {
51 vector<int32_t> ids;
52
53 if (mTunerService != nullptr) {
54 Status s = mTunerService->getFrontendIds(&ids);
55 if (!s.isOk()) {
56 ids.clear();
57 }
58 }
59
60 return ids;
61 }
62
openFrontend(int32_t frontendHandle)63 sp<FrontendClient> TunerClient::openFrontend(int32_t frontendHandle) {
64 if (mTunerService != nullptr) {
65 shared_ptr<ITunerFrontend> tunerFrontend;
66 Status s = mTunerService->openFrontend(frontendHandle, &tunerFrontend);
67 if (!s.isOk() || tunerFrontend == nullptr) {
68 return nullptr;
69 }
70 int32_t id;
71 s = tunerFrontend->getFrontendId(&id);
72 if (!s.isOk()) {
73 tunerFrontend->close();
74 return nullptr;
75 }
76 FrontendInfo frontendInfo;
77 s = mTunerService->getFrontendInfo(id, &frontendInfo);
78 if (!s.isOk()) {
79 tunerFrontend->close();
80 return nullptr;
81 }
82 return new FrontendClient(tunerFrontend, frontendInfo.type);
83 }
84
85 return nullptr;
86 }
87
getFrontendInfo(int32_t id)88 shared_ptr<FrontendInfo> TunerClient::getFrontendInfo(int32_t id) {
89 if (mTunerService != nullptr) {
90 FrontendInfo aidlFrontendInfo;
91 Status s = mTunerService->getFrontendInfo(id, &aidlFrontendInfo);
92 if (!s.isOk()) {
93 return nullptr;
94 }
95 return make_shared<FrontendInfo>(aidlFrontendInfo);
96 }
97
98 return nullptr;
99 }
100
openDemux(int32_t demuxHandle)101 sp<DemuxClient> TunerClient::openDemux(int32_t demuxHandle) {
102 if (mTunerService != nullptr) {
103 shared_ptr<ITunerDemux> tunerDemux;
104 Status s = mTunerService->openDemux(demuxHandle, &tunerDemux);
105 if (!s.isOk()) {
106 return nullptr;
107 }
108 return new DemuxClient(tunerDemux);
109 }
110
111 return nullptr;
112 }
113
getDemuxCaps()114 shared_ptr<DemuxCapabilities> TunerClient::getDemuxCaps() {
115 if (mTunerService != nullptr) {
116 DemuxCapabilities aidlCaps;
117 Status s = mTunerService->getDemuxCaps(&aidlCaps);
118 if (!s.isOk()) {
119 return nullptr;
120 }
121 return make_shared<DemuxCapabilities>(aidlCaps);
122 }
123
124 return nullptr;
125 }
126
openDescrambler(int32_t descramblerHandle)127 sp<DescramblerClient> TunerClient::openDescrambler(int32_t descramblerHandle) {
128 if (mTunerService != nullptr) {
129 shared_ptr<ITunerDescrambler> tunerDescrambler;
130 Status s = mTunerService->openDescrambler(descramblerHandle, &tunerDescrambler);
131 if (!s.isOk()) {
132 return nullptr;
133 }
134 return new DescramblerClient(tunerDescrambler);
135 }
136
137 return nullptr;
138 }
139
openLnb(int32_t lnbHandle)140 sp<LnbClient> TunerClient::openLnb(int32_t lnbHandle) {
141 if (mTunerService != nullptr) {
142 shared_ptr<ITunerLnb> tunerLnb;
143 Status s = mTunerService->openLnb(lnbHandle, &tunerLnb);
144 if (!s.isOk()) {
145 return nullptr;
146 }
147 return new LnbClient(tunerLnb);
148 }
149
150 return nullptr;
151 }
152
openLnbByName(string lnbName)153 sp<LnbClient> TunerClient::openLnbByName(string lnbName) {
154 if (mTunerService != nullptr) {
155 shared_ptr<ITunerLnb> tunerLnb;
156 Status s = mTunerService->openLnbByName(lnbName, &tunerLnb);
157 if (!s.isOk()) {
158 return nullptr;
159 }
160 return new LnbClient(tunerLnb);
161 }
162
163 return nullptr;
164 }
165
openSharedFilter(const string & filterToken,sp<FilterClientCallback> cb)166 sp<FilterClient> TunerClient::openSharedFilter(const string& filterToken,
167 sp<FilterClientCallback> cb) {
168 if (cb == nullptr) {
169 return nullptr;
170 }
171
172 if (mTunerService != nullptr) {
173 shared_ptr<ITunerFilter> tunerFilter;
174 shared_ptr<TunerFilterCallback> callback =
175 ::ndk::SharedRefBase::make<TunerFilterCallback>(cb);
176 Status s = mTunerService->openSharedFilter(filterToken, callback, &tunerFilter);
177 if (!s.isOk()) {
178 return nullptr;
179 }
180 DemuxFilterType type;
181 tunerFilter->getFilterType(&type);
182 return new FilterClient(type, tunerFilter);
183 }
184
185 return nullptr;
186 }
187
setLna(bool bEnable)188 Result TunerClient::setLna(bool bEnable) {
189 if (mTunerService != nullptr) {
190 Status s = mTunerService->setLna(bEnable);
191 return ClientHelper::getServiceSpecificErrorCode(s);
192 }
193
194 return Result::INVALID_STATE;
195 }
196
setMaxNumberOfFrontends(FrontendType frontendType,int32_t maxNumber)197 Result TunerClient::setMaxNumberOfFrontends(FrontendType frontendType, int32_t maxNumber) {
198 if (mTunerService != nullptr) {
199 Status s = mTunerService->setMaxNumberOfFrontends(frontendType, maxNumber);
200 return ClientHelper::getServiceSpecificErrorCode(s);
201 }
202
203 return Result::INVALID_STATE;
204 }
205
getMaxNumberOfFrontends(FrontendType frontendType)206 int TunerClient::getMaxNumberOfFrontends(FrontendType frontendType) {
207 if (mTunerService != nullptr) {
208 int32_t maxNumber;
209 mTunerService->getMaxNumberOfFrontends(frontendType, &maxNumber);
210 return maxNumber;
211 }
212
213 return -1;
214 }
215
216 } // namespace android
217