blob: cbcea911b7e6f3fbad8122980021ab593568fe51 [file] [log] [blame]
shubang23aa3ac2020-09-07 18:56:28 -07001/**
2 * Copyright (c) 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 "TunerService"
18
19#include <android/binder_manager.h>
Devin Moore95bd53d2021-01-21 14:03:40 -080020#include <fmq/ConvertMQDescriptors.h>
shubang23aa3ac2020-09-07 18:56:28 -070021#include <utils/Log.h>
22#include "TunerService.h"
Amy Zhanga046eee2021-01-12 14:44:58 -080023#include "TunerFrontend.h"
24#include "TunerLnb.h"
shubangae56a2e2021-01-21 07:29:55 -080025#include "TunerDemux.h"
shubang23aa3ac2020-09-07 18:56:28 -070026
Amy Zhang70de35a2020-10-12 20:13:16 -070027using ::aidl::android::media::tv::tuner::TunerFrontendAnalogCapabilities;
28using ::aidl::android::media::tv::tuner::TunerFrontendAtsc3Capabilities;
29using ::aidl::android::media::tv::tuner::TunerFrontendAtscCapabilities;
30using ::aidl::android::media::tv::tuner::TunerFrontendCableCapabilities;
31using ::aidl::android::media::tv::tuner::TunerFrontendCapabilities;
32using ::aidl::android::media::tv::tuner::TunerFrontendDvbsCapabilities;
33using ::aidl::android::media::tv::tuner::TunerFrontendDvbtCapabilities;
34using ::aidl::android::media::tv::tuner::TunerFrontendIsdbs3Capabilities;
35using ::aidl::android::media::tv::tuner::TunerFrontendIsdbsCapabilities;
36using ::aidl::android::media::tv::tuner::TunerFrontendIsdbtCapabilities;
shubang23aa3ac2020-09-07 18:56:28 -070037using ::android::hardware::hidl_vec;
shubang6d266262020-10-09 00:15:04 -070038using ::android::hardware::tv::tuner::V1_0::DemuxFilterAvSettings;
39using ::android::hardware::tv::tuner::V1_0::DemuxFilterMainType;
40using ::android::hardware::tv::tuner::V1_0::DemuxFilterSettings;
41using ::android::hardware::tv::tuner::V1_0::DemuxFilterType;
42using ::android::hardware::tv::tuner::V1_0::DemuxTsFilterType;
shubang23aa3ac2020-09-07 18:56:28 -070043using ::android::hardware::tv::tuner::V1_0::FrontendId;
Amy Zhang70de35a2020-10-12 20:13:16 -070044using ::android::hardware::tv::tuner::V1_0::FrontendType;
Amy Zhanga046eee2021-01-12 14:44:58 -080045using ::android::hardware::tv::tuner::V1_0::IFrontend;
46using ::android::hardware::tv::tuner::V1_0::ILnb;
47using ::android::hardware::tv::tuner::V1_0::LnbId;
shubang23aa3ac2020-09-07 18:56:28 -070048using ::android::hardware::tv::tuner::V1_0::Result;
49
50namespace android {
51
shubang23aa3ac2020-09-07 18:56:28 -070052TunerService::TunerService() {}
53TunerService::~TunerService() {}
54
55void TunerService::instantiate() {
Amy Zhanga046eee2021-01-12 14:44:58 -080056 shared_ptr<TunerService> service =
shubang23aa3ac2020-09-07 18:56:28 -070057 ::ndk::SharedRefBase::make<TunerService>();
58 AServiceManager_addService(service->asBinder().get(), getServiceName());
shubang6d266262020-10-09 00:15:04 -070059}
60
shubang6d266262020-10-09 00:15:04 -070061bool TunerService::getITuner() {
62 ALOGD("getITuner");
63 if (mTuner != nullptr) {
64 return true;
65 }
Amy Zhang0f04c452020-10-30 13:36:44 -070066 mTuner = ITuner::getService();
67 if (mTuner == nullptr) {
shubang6d266262020-10-09 00:15:04 -070068 ALOGE("Failed to get ITuner service");
69 return false;
Amy Zhang0f04c452020-10-30 13:36:44 -070070 }
shubang6d266262020-10-09 00:15:04 -070071 return true;
72}
73
shubangae56a2e2021-01-21 07:29:55 -080074Status TunerService::openDemux(
75 int /* demuxHandle */, std::shared_ptr<ITunerDemux>* _aidl_return) {
shubang6d266262020-10-09 00:15:04 -070076 ALOGD("openDemux");
77 if (!getITuner()) {
shubangae56a2e2021-01-21 07:29:55 -080078 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::NOT_INITIALIZED));
shubang6d266262020-10-09 00:15:04 -070079 }
80 if (mDemux != nullptr) {
shubangae56a2e2021-01-21 07:29:55 -080081 *_aidl_return = mDemux->ref<ITunerDemux>();
82 return Status::ok();
shubang6d266262020-10-09 00:15:04 -070083 }
84 Result res;
85 uint32_t id;
shubangae56a2e2021-01-21 07:29:55 -080086 sp<IDemux> demuxSp = nullptr;
shubang6d266262020-10-09 00:15:04 -070087 mTuner->openDemux([&](Result r, uint32_t demuxId, const sp<IDemux>& demux) {
88 demuxSp = demux;
89 id = demuxId;
90 res = r;
91 ALOGD("open demux, id = %d", demuxId);
92 });
93 if (res == Result::SUCCESS) {
shubangae56a2e2021-01-21 07:29:55 -080094 mDemux = ::ndk::SharedRefBase::make<TunerDemux>(demuxSp, id);
95 *_aidl_return = mDemux->ref<ITunerDemux>();
96 return Status::ok();
shubang6d266262020-10-09 00:15:04 -070097 }
98
shubangae56a2e2021-01-21 07:29:55 -080099 ALOGD("open demux failed, res = %d", res);
100 mDemux = nullptr;
101 return Status::fromServiceSpecificError(static_cast<int32_t>(res));
shubang6d266262020-10-09 00:15:04 -0700102}
103
104Result TunerService::configFilter() {
105 ALOGD("configFilter");
106 if (mFilter == NULL) {
107 ALOGD("Failed to configure filter: filter not found");
108 return Result::NOT_INITIALIZED;
109 }
110 DemuxFilterSettings filterSettings;
111 DemuxTsFilterSettings tsFilterSettings {
112 .tpid = 256,
113 };
114 DemuxFilterAvSettings filterAvSettings {
115 .isPassthrough = false,
116 };
117 tsFilterSettings.filterSettings.av(filterAvSettings);
118 filterSettings.ts(tsFilterSettings);
119 Result res = mFilter->configure(filterSettings);
120
121 if (res != Result::SUCCESS) {
122 ALOGD("config filter failed, res = %d", res);
123 return res;
124 }
125
126 Result getQueueDescResult = Result::UNKNOWN_ERROR;
127 mFilter->getQueueDesc(
128 [&](Result r, const MQDescriptorSync<uint8_t>& desc) {
129 mFilterMQDesc = desc;
130 getQueueDescResult = r;
131 ALOGD("getFilterQueueDesc");
132 });
133 if (getQueueDescResult == Result::SUCCESS) {
134 unsafeHidlToAidlMQDescriptor<uint8_t, int8_t, SynchronizedReadWrite>(
135 mFilterMQDesc, &mAidlMQDesc);
Amy Zhanga046eee2021-01-12 14:44:58 -0800136 mAidlMq = new (nothrow) AidlMessageQueue(mAidlMQDesc);
shubang6d266262020-10-09 00:15:04 -0700137 EventFlag::createEventFlag(mAidlMq->getEventFlagWord(), &mEventFlag);
138 } else {
139 ALOGD("get MQDesc failed, res = %d", getQueueDescResult);
140 }
141 return getQueueDescResult;
shubang23aa3ac2020-09-07 18:56:28 -0700142}
143
Amy Zhanga046eee2021-01-12 14:44:58 -0800144Status TunerService::getFrontendIds(vector<int32_t>* ids, int32_t* /* _aidl_return */) {
shubang6d266262020-10-09 00:15:04 -0700145 if (!getITuner()) {
Amy Zhanga046eee2021-01-12 14:44:58 -0800146 return Status::fromServiceSpecificError(
shubang6d266262020-10-09 00:15:04 -0700147 static_cast<int32_t>(Result::NOT_INITIALIZED));
shubang23aa3ac2020-09-07 18:56:28 -0700148 }
149 hidl_vec<FrontendId> feIds;
150 Result res;
151 mTuner->getFrontendIds([&](Result r, const hidl_vec<FrontendId>& frontendIds) {
152 feIds = frontendIds;
153 res = r;
154 });
155 if (res != Result::SUCCESS) {
Amy Zhanga046eee2021-01-12 14:44:58 -0800156 return Status::fromServiceSpecificError(static_cast<int32_t>(res));
shubang23aa3ac2020-09-07 18:56:28 -0700157 }
158 ids->resize(feIds.size());
Amy Zhanga046eee2021-01-12 14:44:58 -0800159 copy(feIds.begin(), feIds.end(), ids->begin());
shubang23aa3ac2020-09-07 18:56:28 -0700160
Amy Zhang0f04c452020-10-30 13:36:44 -0700161 return Status::ok();
shubang23aa3ac2020-09-07 18:56:28 -0700162}
163
Amy Zhang70de35a2020-10-12 20:13:16 -0700164Status TunerService::getFrontendInfo(
Amy Zhang1d28bbb2021-01-13 18:11:15 -0800165 int32_t frontendHandle, TunerFrontendInfo* _aidl_return) {
Amy Zhang70de35a2020-10-12 20:13:16 -0700166 if (mTuner == nullptr) {
Amy Zhang0f04c452020-10-30 13:36:44 -0700167 ALOGE("ITuner service is not init.");
168 return ::ndk::ScopedAStatus::fromServiceSpecificError(
169 static_cast<int32_t>(Result::UNAVAILABLE));
Amy Zhang70de35a2020-10-12 20:13:16 -0700170 }
171
172 Result res;
173 FrontendInfo info;
Amy Zhanga046eee2021-01-12 14:44:58 -0800174 int feId = getResourceIdFromHandle(frontendHandle, FRONTEND);
Amy Zhang70de35a2020-10-12 20:13:16 -0700175 mTuner->getFrontendInfo(feId, [&](Result r, const FrontendInfo& feInfo) {
176 info = feInfo;
177 res = r;
178 });
179 if (res != Result::SUCCESS) {
Amy Zhang0f04c452020-10-30 13:36:44 -0700180 return Status::fromServiceSpecificError(static_cast<int32_t>(res));
Amy Zhang70de35a2020-10-12 20:13:16 -0700181 }
182
Amy Zhang1d28bbb2021-01-13 18:11:15 -0800183 TunerFrontendInfo tunerInfo = convertToAidlFrontendInfo(info);
Amy Zhang70de35a2020-10-12 20:13:16 -0700184 *_aidl_return = tunerInfo;
Amy Zhang0f04c452020-10-30 13:36:44 -0700185 return Status::ok();
186}
187
188Status TunerService::openFrontend(
Amy Zhanga046eee2021-01-12 14:44:58 -0800189 int32_t frontendHandle, shared_ptr<ITunerFrontend>* _aidl_return) {
Amy Zhang0f04c452020-10-30 13:36:44 -0700190 if (mTuner == nullptr) {
191 ALOGE("ITuner service is not init.");
Amy Zhanga046eee2021-01-12 14:44:58 -0800192 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
Amy Zhang0f04c452020-10-30 13:36:44 -0700193 }
194
Amy Zhanga046eee2021-01-12 14:44:58 -0800195 Result status;
196 sp<IFrontend> frontend;
197 int id = getResourceIdFromHandle(frontendHandle, FRONTEND);
198 mTuner->openFrontendById(id, [&](Result result, const sp<IFrontend>& fe) {
199 frontend = fe;
200 status = result;
201 });
202 if (status != Result::SUCCESS) {
203 return Status::fromServiceSpecificError(static_cast<int32_t>(status));
204 }
205 *_aidl_return = ::ndk::SharedRefBase::make<TunerFrontend>(frontend, id);
206 return Status::ok();
207}
208
209Status TunerService::getFmqSyncReadWrite(
210 MQDescriptor<int8_t, SynchronizedReadWrite>* mqDesc, bool* _aidl_return) {
211 ALOGD("getFmqSyncReadWrite");
212 // TODO: put the following methods AIDL, and should be called from clients.
Amy Zhanga046eee2021-01-12 14:44:58 -0800213 configFilter();
214 mFilter->start();
215 if (mqDesc == nullptr) {
216 ALOGD("getFmqSyncReadWrite null MQDescriptor.");
217 *_aidl_return = false;
218 } else {
219 ALOGD("getFmqSyncReadWrite true");
220 *_aidl_return = true;
221 *mqDesc = move(mAidlMQDesc);
222 }
223 return ndk::ScopedAStatus::ok();
224}
225
226Status TunerService::openLnb(int lnbHandle, shared_ptr<ITunerLnb>* _aidl_return) {
227 if (mTuner == nullptr) {
228 ALOGE("ITuner service is not init.");
229 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
230 }
231
232 Result status;
233 sp<ILnb> lnb;
234 int id = getResourceIdFromHandle(lnbHandle, LNB);
235 mTuner->openLnbById(id, [&](Result result, const sp<ILnb>& lnbSp){
236 lnb = lnbSp;
237 status = result;
238 });
239 if (status != Result::SUCCESS) {
240 return Status::fromServiceSpecificError(static_cast<int32_t>(status));
241 }
242
243 *_aidl_return = ::ndk::SharedRefBase::make<TunerLnb>(lnb, id);
244 return Status::ok();
245}
246
247Status TunerService::openLnbByName(const string& lnbName, shared_ptr<ITunerLnb>* _aidl_return) {
248 if (mTuner == nullptr) {
249 ALOGE("ITuner service is not init.");
250 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
251 }
252
253 int lnbId;
254 Result status;
255 sp<ILnb> lnb;
256 mTuner->openLnbByName(lnbName, [&](Result r, LnbId id, const sp<ILnb>& lnbSp) {
257 status = r;
258 lnb = lnbSp;
259 lnbId = (int)id;
260 });
261 if (status != Result::SUCCESS) {
262 return Status::fromServiceSpecificError(static_cast<int32_t>(status));
263 }
264
265 *_aidl_return = ::ndk::SharedRefBase::make<TunerLnb>(lnb, lnbId);
Amy Zhang0f04c452020-10-30 13:36:44 -0700266 return Status::ok();
Amy Zhang70de35a2020-10-12 20:13:16 -0700267}
268
Amy Zhang1d28bbb2021-01-13 18:11:15 -0800269TunerFrontendInfo TunerService::convertToAidlFrontendInfo(FrontendInfo halInfo) {
270 TunerFrontendInfo info{
Amy Zhang70de35a2020-10-12 20:13:16 -0700271 .type = (int)halInfo.type,
272 .minFrequency = (int)halInfo.minFrequency,
273 .maxFrequency = (int)halInfo.maxFrequency,
274 .minSymbolRate = (int)halInfo.minSymbolRate,
275 .maxSymbolRate = (int)halInfo.maxSymbolRate,
276 .acquireRange = (int)halInfo.acquireRange,
277 .exclusiveGroupId = (int)halInfo.exclusiveGroupId,
278 };
279 for (int i = 0; i < halInfo.statusCaps.size(); i++) {
280 info.statusCaps.push_back((int)halInfo.statusCaps[i]);
281 }
282
283 TunerFrontendCapabilities caps;
284 switch (halInfo.type) {
285 case FrontendType::ANALOG: {
286 TunerFrontendAnalogCapabilities analogCaps{
287 .typeCap = (int)halInfo.frontendCaps.analogCaps().typeCap,
288 .sifStandardCap = (int)halInfo.frontendCaps.analogCaps().sifStandardCap,
289 };
290 caps.set<TunerFrontendCapabilities::analogCaps>(analogCaps);
291 break;
292 }
293 case FrontendType::ATSC: {
294 TunerFrontendAtscCapabilities atscCaps{
295 .modulationCap = (int)halInfo.frontendCaps.atscCaps().modulationCap,
296 };
297 caps.set<TunerFrontendCapabilities::atscCaps>(atscCaps);
298 break;
299 }
300 case FrontendType::ATSC3: {
301 TunerFrontendAtsc3Capabilities atsc3Caps{
302 .bandwidthCap = (int)halInfo.frontendCaps.atsc3Caps().bandwidthCap,
303 .modulationCap = (int)halInfo.frontendCaps.atsc3Caps().modulationCap,
304 .timeInterleaveModeCap =
305 (int)halInfo.frontendCaps.atsc3Caps().timeInterleaveModeCap,
306 .codeRateCap = (int)halInfo.frontendCaps.atsc3Caps().codeRateCap,
307 .demodOutputFormatCap = (int)halInfo.frontendCaps.atsc3Caps().demodOutputFormatCap,
308 .fecCap = (int)halInfo.frontendCaps.atsc3Caps().fecCap,
309 };
310 caps.set<TunerFrontendCapabilities::atsc3Caps>(atsc3Caps);
311 break;
312 }
313 case FrontendType::DVBC: {
314 TunerFrontendCableCapabilities cableCaps{
315 .modulationCap = (int)halInfo.frontendCaps.dvbcCaps().modulationCap,
316 .codeRateCap = (int)halInfo.frontendCaps.dvbcCaps().fecCap,
317 .annexCap = (int)halInfo.frontendCaps.dvbcCaps().annexCap,
318 };
319 caps.set<TunerFrontendCapabilities::cableCaps>(cableCaps);
320 break;
321 }
322 case FrontendType::DVBS: {
323 TunerFrontendDvbsCapabilities dvbsCaps{
324 .modulationCap = (int)halInfo.frontendCaps.dvbsCaps().modulationCap,
325 .codeRateCap = (long)halInfo.frontendCaps.dvbsCaps().innerfecCap,
326 .standard = (int)halInfo.frontendCaps.dvbsCaps().standard,
327 };
328 caps.set<TunerFrontendCapabilities::dvbsCaps>(dvbsCaps);
329 break;
330 }
331 case FrontendType::DVBT: {
332 TunerFrontendDvbtCapabilities dvbtCaps{
333 .transmissionModeCap = (int)halInfo.frontendCaps.dvbtCaps().transmissionModeCap,
334 .bandwidthCap = (int)halInfo.frontendCaps.dvbtCaps().bandwidthCap,
335 .constellationCap = (int)halInfo.frontendCaps.dvbtCaps().constellationCap,
336 .codeRateCap = (int)halInfo.frontendCaps.dvbtCaps().coderateCap,
337 .hierarchyCap = (int)halInfo.frontendCaps.dvbtCaps().hierarchyCap,
338 .guardIntervalCap = (int)halInfo.frontendCaps.dvbtCaps().guardIntervalCap,
339 .isT2Supported = (bool)halInfo.frontendCaps.dvbtCaps().isT2Supported,
340 .isMisoSupported = (bool)halInfo.frontendCaps.dvbtCaps().isMisoSupported,
341 };
342 caps.set<TunerFrontendCapabilities::dvbtCaps>(dvbtCaps);
343 break;
344 }
345 case FrontendType::ISDBS: {
346 TunerFrontendIsdbsCapabilities isdbsCaps{
347 .modulationCap = (int)halInfo.frontendCaps.isdbsCaps().modulationCap,
348 .codeRateCap = (int)halInfo.frontendCaps.isdbsCaps().coderateCap,
349 };
350 caps.set<TunerFrontendCapabilities::isdbsCaps>(isdbsCaps);
351 break;
352 }
353 case FrontendType::ISDBS3: {
354 TunerFrontendIsdbs3Capabilities isdbs3Caps{
355 .modulationCap = (int)halInfo.frontendCaps.isdbs3Caps().modulationCap,
356 .codeRateCap = (int)halInfo.frontendCaps.isdbs3Caps().coderateCap,
357 };
358 caps.set<TunerFrontendCapabilities::isdbs3Caps>(isdbs3Caps);
359 break;
360 }
361 case FrontendType::ISDBT: {
362 TunerFrontendIsdbtCapabilities isdbtCaps{
363 .modeCap = (int)halInfo.frontendCaps.isdbtCaps().modeCap,
364 .bandwidthCap = (int)halInfo.frontendCaps.isdbtCaps().bandwidthCap,
365 .modulationCap = (int)halInfo.frontendCaps.isdbtCaps().modulationCap,
366 .codeRateCap = (int)halInfo.frontendCaps.isdbtCaps().coderateCap,
367 .guardIntervalCap = (int)halInfo.frontendCaps.isdbtCaps().guardIntervalCap,
368 };
369 caps.set<TunerFrontendCapabilities::isdbtCaps>(isdbtCaps);
370 break;
371 }
372 default:
373 break;
374 }
375
376 info.caps = caps;
377 return info;
378}
shubang23aa3ac2020-09-07 18:56:28 -0700379} // namespace android