• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "FilterClient"
18 
19 #include <aidlcommonsupport/NativeHandle.h>
20 #include <android-base/logging.h>
21 #include <fmq/ConvertMQDescriptors.h>
22 #include <utils/Log.h>
23 
24 #include "FilterClient.h"
25 
26 using ::aidl::android::media::tv::tuner::TunerDemuxIpAddressSettings;
27 using ::aidl::android::media::tv::tuner::TunerFilterAlpConfiguration;
28 using ::aidl::android::media::tv::tuner::TunerFilterIpConfiguration;
29 using ::aidl::android::media::tv::tuner::TunerFilterMmtpConfiguration;
30 using ::aidl::android::media::tv::tuner::TunerFilterMonitorEvent;
31 using ::aidl::android::media::tv::tuner::TunerFilterScIndexMask;
32 using ::aidl::android::media::tv::tuner::TunerFilterSectionBits;
33 using ::aidl::android::media::tv::tuner::TunerFilterSectionCondition;
34 using ::aidl::android::media::tv::tuner::TunerFilterSectionTableInfo;
35 using ::aidl::android::media::tv::tuner::TunerFilterSharedHandleInfo;
36 using ::aidl::android::media::tv::tuner::TunerFilterTlvConfiguration;
37 using ::aidl::android::media::tv::tuner::TunerFilterTsConfiguration;
38 using ::android::hardware::hidl_vec;
39 using ::android::hardware::tv::tuner::V1_0::DemuxFilterMainType;
40 using ::android::hardware::tv::tuner::V1_0::DemuxMmtpFilterType;
41 using ::android::hardware::tv::tuner::V1_0::DemuxQueueNotifyBits;
42 using ::android::hardware::tv::tuner::V1_0::DemuxStreamId;
43 using ::android::hardware::tv::tuner::V1_0::DemuxTpid;
44 using ::android::hardware::tv::tuner::V1_0::DemuxTsFilterSettings;
45 using ::android::hardware::tv::tuner::V1_0::DemuxTsFilterType;
46 using ::android::hardware::tv::tuner::V1_1::DemuxFilterMonitorEvent;
47 using ::android::hardware::tv::tuner::V1_1::ScramblingStatus;
48 
49 namespace android {
50 
51 /////////////// FilterClient ///////////////////////
52 
FilterClient(DemuxFilterType type,shared_ptr<ITunerFilter> tunerFilter)53 FilterClient::FilterClient(DemuxFilterType type, shared_ptr<ITunerFilter> tunerFilter) {
54     mTunerFilter = tunerFilter;
55     mAvSharedHandle = NULL;
56     checkIsMediaFilter(type);
57 }
58 
~FilterClient()59 FilterClient::~FilterClient() {
60     mTunerFilter = NULL;
61     mFilter = NULL;
62     mFilter_1_1 = NULL;
63     mAvSharedHandle = NULL;
64     mAvSharedMemSize = 0;
65     mIsMediaFilter = false;
66     mIsPassthroughFilter = false;
67     mFilterMQ = NULL;
68     mFilterMQEventFlag = NULL;
69 }
70 
71 // TODO: remove after migration to Tuner Service is done.
setHidlFilter(sp<IFilter> filter)72 void FilterClient::setHidlFilter(sp<IFilter> filter) {
73     mFilter = filter;
74     mFilter_1_1 = ::android::hardware::tv::tuner::V1_1::IFilter::castFrom(mFilter);
75 }
76 
read(int8_t * buffer,int size)77 int FilterClient::read(int8_t* buffer, int size) {
78     Result res = getFilterMq();
79     if (res != Result::SUCCESS) {
80         return -1;
81     }
82     return copyData(buffer, size);
83 }
84 
getAvSharedHandleInfo()85 SharedHandleInfo FilterClient::getAvSharedHandleInfo() {
86     handleAvShareMemory();
87     SharedHandleInfo info{
88         .sharedHandle = (mIsMediaFilter && !mIsPassthroughFilter) ? mAvSharedHandle : NULL,
89         .size = mAvSharedMemSize,
90     };
91 
92     return info;
93 }
94 
configure(DemuxFilterSettings configure)95 Result FilterClient::configure(DemuxFilterSettings configure) {
96     Result res;
97     checkIsPassthroughFilter(configure);
98 
99     if (mTunerFilter != NULL) {
100         Status s = mTunerFilter->configure(getAidlFilterSettings(configure));
101         res = ClientHelper::getServiceSpecificErrorCode(s);
102         if (res == Result::SUCCESS) {
103             getAvSharedHandleInfo();
104         }
105         return res;
106     }
107 
108     if (mFilter != NULL) {
109         res = mFilter->configure(configure);
110         if (res == Result::SUCCESS) {
111             getAvSharedHandleInfo();
112         }
113         return res;
114     }
115 
116     return Result::INVALID_STATE;
117 }
118 
configureMonitorEvent(int monitorEventType)119 Result FilterClient::configureMonitorEvent(int monitorEventType) {
120     if (mTunerFilter != NULL) {
121         Status s = mTunerFilter->configureMonitorEvent(monitorEventType);
122         return ClientHelper::getServiceSpecificErrorCode(s);
123     }
124 
125     if (mFilter_1_1 != NULL) {
126         return mFilter_1_1->configureMonitorEvent(monitorEventType);
127     }
128 
129     return Result::INVALID_STATE;
130 }
131 
configureIpFilterContextId(int cid)132 Result FilterClient::configureIpFilterContextId(int cid) {
133     if (mTunerFilter != NULL) {
134         Status s = mTunerFilter->configureIpFilterContextId(cid);
135         return ClientHelper::getServiceSpecificErrorCode(s);
136     }
137 
138     if (mFilter_1_1 != NULL) {
139         return mFilter_1_1->configureIpCid(cid);
140     }
141 
142     return Result::INVALID_STATE;
143 }
144 
configureAvStreamType(AvStreamType avStreamType)145 Result FilterClient::configureAvStreamType(AvStreamType avStreamType) {
146     if (mTunerFilter != NULL) {
147         int type;
148         switch (avStreamType.getDiscriminator()) {
149             case AvStreamType::hidl_discriminator::audio:
150                 type = (int)avStreamType.audio();
151                 break;
152             case AvStreamType::hidl_discriminator::video:
153                 type = (int)avStreamType.video();
154                 break;
155         }
156         Status s = mTunerFilter->configureAvStreamType(type);
157         return ClientHelper::getServiceSpecificErrorCode(s);
158     }
159 
160     if (mFilter_1_1 != NULL) {
161         return mFilter_1_1->configureAvStreamType(avStreamType);
162     }
163 
164     return Result::INVALID_STATE;
165 }
166 
start()167 Result FilterClient::start() {
168     if (mTunerFilter != NULL) {
169         Status s = mTunerFilter->start();
170         return ClientHelper::getServiceSpecificErrorCode(s);
171     }
172 
173     if (mFilter != NULL) {
174         return mFilter->start();
175     }
176 
177     return Result::INVALID_STATE;
178 }
179 
stop()180 Result FilterClient::stop() {
181     if (mTunerFilter != NULL) {
182         Status s = mTunerFilter->stop();
183         return ClientHelper::getServiceSpecificErrorCode(s);
184     }
185 
186     if (mFilter != NULL) {
187         return mFilter->stop();
188     }
189 
190     return Result::INVALID_STATE;
191 }
192 
flush()193 Result FilterClient::flush() {
194     if (mTunerFilter != NULL) {
195         Status s = mTunerFilter->flush();
196         return ClientHelper::getServiceSpecificErrorCode(s);
197     }
198 
199     if (mFilter != NULL) {
200         return mFilter->flush();
201     }
202 
203     return Result::INVALID_STATE;
204 }
205 
getId(uint32_t & id)206 Result FilterClient::getId(uint32_t& id) {
207     if (mTunerFilter != NULL) {
208         int32_t id32Bit;
209         Status s = mTunerFilter->getId(&id32Bit);
210         id = static_cast<uint32_t>(id32Bit);
211         return ClientHelper::getServiceSpecificErrorCode(s);
212     }
213 
214     if (mFilter != NULL) {
215         Result res;
216         mFilter->getId([&](Result r, uint32_t filterId) {
217             res = r;
218             id = filterId;
219         });
220         return res;
221     }
222 
223     return Result::INVALID_STATE;
224 }
225 
getId64Bit(uint64_t & id)226 Result FilterClient::getId64Bit(uint64_t& id) {
227     if (mTunerFilter != NULL) {
228         int64_t id64Bit;
229         Status s = mTunerFilter->getId64Bit(&id64Bit);
230         id = static_cast<uint64_t>(id64Bit);
231         return ClientHelper::getServiceSpecificErrorCode(s);
232     }
233 
234     if (mFilter_1_1 != NULL) {
235         Result res;
236         mFilter_1_1->getId64Bit([&](Result r, uint64_t filterId) {
237             res = r;
238             id = filterId;
239         });
240         return res;
241     }
242 
243     return Result::INVALID_STATE;
244 }
245 
releaseAvHandle(native_handle_t * handle,uint64_t avDataId)246 Result FilterClient::releaseAvHandle(native_handle_t* handle, uint64_t avDataId) {
247     if (mTunerFilter != NULL) {
248         Status s = mTunerFilter->releaseAvHandle(makeToAidl(handle), avDataId);
249         return ClientHelper::getServiceSpecificErrorCode(s);
250     }
251 
252     if (mFilter != NULL) {
253         return mFilter->releaseAvHandle(hidl_handle(handle), avDataId);
254     }
255 
256     return Result::INVALID_STATE;
257 }
258 
setDataSource(sp<FilterClient> filterClient)259 Result FilterClient::setDataSource(sp<FilterClient> filterClient){
260     if (mTunerFilter != NULL) {
261         Status s = mTunerFilter->setDataSource(filterClient->getAidlFilter());
262         return ClientHelper::getServiceSpecificErrorCode(s);
263     }
264 
265     if (mFilter != NULL) {
266         sp<IFilter> sourceFilter = filterClient->getHalFilter();
267         if (sourceFilter == NULL) {
268             return Result::INVALID_ARGUMENT;
269         }
270         return mFilter->setDataSource(sourceFilter);
271     }
272 
273     return Result::INVALID_STATE;
274 }
275 
close()276 Result FilterClient::close() {
277     if (mFilterMQEventFlag) {
278         EventFlag::deleteEventFlag(&mFilterMQEventFlag);
279     }
280     mFilterMQEventFlag = NULL;
281     mFilterMQ = NULL;
282 
283     if (mTunerFilter != NULL) {
284         Status s = mTunerFilter->close();
285         closeAvSharedMemory();
286         mTunerFilter = NULL;
287         return ClientHelper::getServiceSpecificErrorCode(s);
288     }
289 
290     if (mFilter != NULL) {
291         Result res = mFilter->close();
292         mFilter = NULL;
293         mFilter_1_1 = NULL;
294         closeAvSharedMemory();
295         return res;
296     }
297 
298     return Result::INVALID_STATE;
299 }
300 
301 /////////////// IFilterCallback ///////////////////////
302 
HidlFilterCallback(sp<FilterClientCallback> filterClientCallback)303 HidlFilterCallback::HidlFilterCallback(sp<FilterClientCallback> filterClientCallback)
304         : mFilterClientCallback(filterClientCallback) {}
305 
onFilterStatus(const DemuxFilterStatus status)306 Return<void> HidlFilterCallback::onFilterStatus(const DemuxFilterStatus status) {
307     if (mFilterClientCallback != NULL) {
308         mFilterClientCallback->onFilterStatus(status);
309     }
310     return Void();
311 }
312 
onFilterEvent(const DemuxFilterEvent & filterEvent)313 Return<void> HidlFilterCallback::onFilterEvent(const DemuxFilterEvent& filterEvent) {
314     if (mFilterClientCallback != NULL) {
315         mFilterClientCallback->onFilterEvent(filterEvent);
316     }
317     return Void();
318 }
319 
onFilterEvent_1_1(const DemuxFilterEvent & filterEvent,const DemuxFilterEventExt & filterEventExt)320 Return<void> HidlFilterCallback::onFilterEvent_1_1(const DemuxFilterEvent& filterEvent,
321         const DemuxFilterEventExt& filterEventExt) {
322     if (mFilterClientCallback != NULL) {
323         mFilterClientCallback->onFilterEvent_1_1(filterEvent, filterEventExt);
324     }
325     return Void();
326 }
327 
328 /////////////// TunerFilterCallback ///////////////////////
329 
TunerFilterCallback(sp<FilterClientCallback> filterClientCallback)330 TunerFilterCallback::TunerFilterCallback(sp<FilterClientCallback> filterClientCallback)
331         : mFilterClientCallback(filterClientCallback) {}
332 
onFilterStatus(int status)333 Status TunerFilterCallback::onFilterStatus(int status) {
334     if (mFilterClientCallback != NULL) {
335         mFilterClientCallback->onFilterStatus(static_cast<DemuxFilterStatus>(status));
336         return Status::ok();
337     }
338     return Status::fromServiceSpecificError(static_cast<int32_t>(Result::INVALID_STATE));
339 }
340 
onFilterEvent(const vector<TunerFilterEvent> & filterEvents)341 Status TunerFilterCallback::onFilterEvent(const vector<TunerFilterEvent>& filterEvents) {
342     if (mFilterClientCallback == NULL) {
343         return Status::fromServiceSpecificError(static_cast<int32_t>(Result::INVALID_STATE));
344     }
345 
346     if (filterEvents.size() == 0) {
347         return Status::fromServiceSpecificError(static_cast<int32_t>(Result::INVALID_ARGUMENT));
348     }
349 
350     DemuxFilterEvent event;
351     DemuxFilterEventExt eventExt;
352     getHidlFilterEvent(filterEvents, event, eventExt);
353     if (eventExt.events.size() > 0) {
354         mFilterClientCallback->onFilterEvent_1_1(event, eventExt);
355     } else {
356         mFilterClientCallback->onFilterEvent(event);
357     }
358 
359     return Status::ok();
360 }
361 
362 /////////////// FilterClient Helper Methods ///////////////////////
363 
getAidlFilterSettings(DemuxFilterSettings configure)364 TunerFilterConfiguration FilterClient::getAidlFilterSettings(DemuxFilterSettings configure) {
365     TunerFilterConfiguration config;
366     switch (configure.getDiscriminator()) {
367         case DemuxFilterSettings::hidl_discriminator::ts:
368             return getAidlTsSettings(configure.ts());
369         case DemuxFilterSettings::hidl_discriminator::mmtp:
370             return getAidlMmtpSettings(configure.mmtp());
371         case DemuxFilterSettings::hidl_discriminator::ip:
372             return getAidlIpSettings(configure.ip());
373         case DemuxFilterSettings::hidl_discriminator::tlv:
374             return getAidlTlvSettings(configure.tlv());
375         case DemuxFilterSettings::hidl_discriminator::alp:
376             return getAidlAlpSettings(configure.alp());
377         default:
378             break;
379     }
380     ALOGE("Wrong DemuxFilterSettings union.");
381     return config;
382 }
383 
getAidlTsSettings(DemuxTsFilterSettings ts)384 TunerFilterConfiguration FilterClient::getAidlTsSettings(DemuxTsFilterSettings ts) {
385     TunerFilterConfiguration config;
386     TunerFilterSettings filterSettings;
387     switch (ts.filterSettings.getDiscriminator()) {
388         case DemuxTsFilterSettings::FilterSettings::hidl_discriminator::av: {
389             filterSettings.set<TunerFilterSettings::av>(
390                     getAidlAvSettings(ts.filterSettings.av()));
391             break;
392         }
393         case DemuxTsFilterSettings::FilterSettings::hidl_discriminator::section: {
394             filterSettings.set<TunerFilterSettings::section>(
395                     getAidlSectionSettings(ts.filterSettings.section()));
396             break;
397         }
398         case DemuxTsFilterSettings::FilterSettings::hidl_discriminator::pesData: {
399             filterSettings.set<TunerFilterSettings::pesData>(
400                     getAidlPesDataSettings(ts.filterSettings.pesData()));
401             break;
402         }
403         case DemuxTsFilterSettings::FilterSettings::hidl_discriminator::record: {
404             filterSettings.set<TunerFilterSettings::record>(
405                     getAidlRecordSettings(ts.filterSettings.record()));
406             break;
407         }
408         default:
409             filterSettings.set<TunerFilterSettings::nothing>(true);
410             break;
411     }
412 
413     TunerFilterTsConfiguration aidlTs{
414         .tpid = static_cast<char16_t>(ts.tpid),
415         .filterSettings = filterSettings,
416     };
417     config.set<TunerFilterConfiguration::ts>(aidlTs);
418 
419     return config;
420 }
421 
getAidlMmtpSettings(DemuxMmtpFilterSettings mmtp)422 TunerFilterConfiguration FilterClient::getAidlMmtpSettings(DemuxMmtpFilterSettings mmtp) {
423     TunerFilterConfiguration config;
424     TunerFilterSettings filterSettings;
425     switch (mmtp.filterSettings.getDiscriminator()) {
426         case DemuxMmtpFilterSettings::FilterSettings::hidl_discriminator::av: {
427             filterSettings.set<TunerFilterSettings::av>(
428                     getAidlAvSettings(mmtp.filterSettings.av()));
429             break;
430         }
431         case DemuxMmtpFilterSettings::FilterSettings::hidl_discriminator::section: {
432             filterSettings.set<TunerFilterSettings::section>(
433                     getAidlSectionSettings(mmtp.filterSettings.section()));
434             break;
435         }
436         case DemuxMmtpFilterSettings::FilterSettings::hidl_discriminator::pesData: {
437             filterSettings.set<TunerFilterSettings::pesData>(
438                     getAidlPesDataSettings(mmtp.filterSettings.pesData()));
439             break;
440         }
441         case DemuxMmtpFilterSettings::FilterSettings::hidl_discriminator::record: {
442             filterSettings.set<TunerFilterSettings::record>(
443                     getAidlRecordSettings(mmtp.filterSettings.record()));
444             break;
445         }
446         case DemuxMmtpFilterSettings::FilterSettings::hidl_discriminator::download: {
447             filterSettings.set<TunerFilterSettings::download>(
448                     getAidlDownloadSettings(mmtp.filterSettings.download()));
449             break;
450         }
451         default:
452             filterSettings.set<TunerFilterSettings::nothing>(true);
453             break;
454     }
455 
456     TunerFilterMmtpConfiguration aidlMmtp{
457         .mmtpPid = static_cast<char16_t>(mmtp.mmtpPid),
458         .filterSettings = filterSettings,
459     };
460     config.set<TunerFilterConfiguration::mmtp>(aidlMmtp);
461 
462     return config;
463 }
464 
getAidlIpSettings(DemuxIpFilterSettings ip)465 TunerFilterConfiguration FilterClient::getAidlIpSettings(DemuxIpFilterSettings ip) {
466     TunerFilterConfiguration config;
467     TunerFilterSettings filterSettings;
468     switch (ip.filterSettings.getDiscriminator()) {
469         case DemuxIpFilterSettings::FilterSettings::hidl_discriminator::section: {
470             filterSettings.set<TunerFilterSettings::section>(
471                     getAidlSectionSettings(ip.filterSettings.section()));
472             break;
473         }
474         case DemuxIpFilterSettings::FilterSettings::hidl_discriminator::bPassthrough: {
475             filterSettings.set<TunerFilterSettings::isPassthrough>(
476                     ip.filterSettings.bPassthrough());
477             break;
478         }
479         default:
480             filterSettings.set<TunerFilterSettings::nothing>(true);
481             break;
482     }
483 
484     TunerDemuxIpAddressSettings ipAddr{
485         .srcPort = static_cast<char16_t>(ip.ipAddr.srcPort),
486         .dstPort = static_cast<char16_t>(ip.ipAddr.dstPort),
487     };
488     getAidlIpAddress(ip.ipAddr, ipAddr.srcIpAddress, ipAddr.dstIpAddress);
489 
490     TunerFilterIpConfiguration aidlIp{
491         .ipAddr = ipAddr,
492         .filterSettings = filterSettings,
493     };
494     config.set<TunerFilterConfiguration::ip>(aidlIp);
495 
496     return config;
497 }
498 
getAidlIpAddress(DemuxIpAddress ipAddr,TunerDemuxIpAddress & srcIpAddress,TunerDemuxIpAddress & dstIpAddress)499 void FilterClient::getAidlIpAddress(DemuxIpAddress ipAddr,
500         TunerDemuxIpAddress& srcIpAddress, TunerDemuxIpAddress& dstIpAddress) {
501     switch (ipAddr.srcIpAddress.getDiscriminator()) {
502         case DemuxIpAddress::SrcIpAddress::hidl_discriminator::v4: {
503             int size = ipAddr.srcIpAddress.v4().size();
504             srcIpAddress.isIpV6 = false;
505             srcIpAddress.addr.resize(size);
506             copy(&ipAddr.srcIpAddress.v4()[0], &ipAddr.srcIpAddress.v4()[size],
507                     srcIpAddress.addr.begin());
508             break;
509         }
510         case DemuxIpAddress::SrcIpAddress::hidl_discriminator::v6: {
511             int size = ipAddr.srcIpAddress.v6().size();
512             srcIpAddress.isIpV6 = true;
513             srcIpAddress.addr.resize(size);
514             copy(&ipAddr.srcIpAddress.v6()[0], &ipAddr.srcIpAddress.v6()[size],
515                     srcIpAddress.addr.begin());
516             break;
517         }
518     }
519     switch (ipAddr.dstIpAddress.getDiscriminator()) {
520         case DemuxIpAddress::DstIpAddress::hidl_discriminator::v4: {
521             int size = ipAddr.dstIpAddress.v4().size();
522             dstIpAddress.isIpV6 = false;
523             dstIpAddress.addr.resize(size);
524             copy(&ipAddr.dstIpAddress.v4()[0], &ipAddr.dstIpAddress.v4()[size],
525                     dstIpAddress.addr.begin());
526             break;
527         }
528         case DemuxIpAddress::DstIpAddress::hidl_discriminator::v6: {
529             int size = ipAddr.dstIpAddress.v6().size();
530             dstIpAddress.isIpV6 = true;
531             dstIpAddress.addr.resize(size);
532             copy(&ipAddr.dstIpAddress.v6()[0], &ipAddr.dstIpAddress.v6()[size],
533                     dstIpAddress.addr.begin());
534             break;
535         }
536     }
537 }
538 
getAidlTlvSettings(DemuxTlvFilterSettings tlv)539 TunerFilterConfiguration FilterClient::getAidlTlvSettings(DemuxTlvFilterSettings tlv) {
540     TunerFilterConfiguration config;
541     TunerFilterSettings filterSettings;
542     switch (tlv.filterSettings.getDiscriminator()) {
543         case DemuxTlvFilterSettings::FilterSettings::hidl_discriminator::section: {
544             filterSettings.set<TunerFilterSettings::section>(
545                     getAidlSectionSettings(tlv.filterSettings.section()));
546             break;
547         }
548         case DemuxTlvFilterSettings::FilterSettings::hidl_discriminator::bPassthrough: {
549             filterSettings.set<TunerFilterSettings::isPassthrough>(
550                     tlv.filterSettings.bPassthrough());
551             break;
552         }
553         default:
554             filterSettings.set<TunerFilterSettings::nothing>(true);
555             break;
556     }
557 
558     TunerFilterTlvConfiguration aidlTlv{
559         .packetType = static_cast<int8_t>(tlv.packetType),
560         .isCompressedIpPacket = tlv.isCompressedIpPacket,
561         .filterSettings = filterSettings,
562     };
563     config.set<TunerFilterConfiguration::tlv>(aidlTlv);
564 
565     return config;
566 }
567 
getAidlAlpSettings(DemuxAlpFilterSettings alp)568 TunerFilterConfiguration FilterClient::getAidlAlpSettings(DemuxAlpFilterSettings alp) {
569     TunerFilterConfiguration config;
570     TunerFilterSettings filterSettings;
571     switch (alp.filterSettings.getDiscriminator()) {
572         case DemuxAlpFilterSettings::FilterSettings::hidl_discriminator::section: {
573             filterSettings.set<TunerFilterSettings::section>(
574                     getAidlSectionSettings(alp.filterSettings.section()));
575             break;
576         }
577         default:
578             filterSettings.set<TunerFilterSettings::nothing>(true);
579             break;
580     }
581 
582     TunerFilterAlpConfiguration aidlAlp{
583         .packetType = static_cast<int8_t>(alp.packetType),
584         .lengthType = static_cast<int8_t>(alp.lengthType),
585         .filterSettings = filterSettings,
586     };
587     config.set<TunerFilterConfiguration::alp>(aidlAlp);
588 
589     return config;
590 }
591 
getAidlAvSettings(DemuxFilterAvSettings hidlAv)592 TunerFilterAvSettings FilterClient::getAidlAvSettings(DemuxFilterAvSettings hidlAv) {
593     TunerFilterAvSettings aidlAv{
594         .isPassthrough = hidlAv.isPassthrough,
595     };
596     return aidlAv;
597 }
598 
getAidlSectionSettings(DemuxFilterSectionSettings hidlSection)599 TunerFilterSectionSettings FilterClient::getAidlSectionSettings(
600         DemuxFilterSectionSettings hidlSection) {
601     TunerFilterSectionSettings aidlSection;
602 
603     switch (hidlSection.condition.getDiscriminator()) {
604         case DemuxFilterSectionSettings::Condition::hidl_discriminator::sectionBits: {
605             TunerFilterSectionBits sectionBits;
606             auto hidlSectionBits = hidlSection.condition.sectionBits();
607             sectionBits.filter.resize(hidlSectionBits.filter.size());
608             sectionBits.mask.resize(hidlSectionBits.mask.size());
609             sectionBits.mode.resize(hidlSectionBits.mode.size());
610             copy(hidlSectionBits.filter.begin(), hidlSectionBits.filter.end(),
611                     sectionBits.filter.begin());
612             copy(hidlSectionBits.mask.begin(), hidlSectionBits.mask.end(),
613                     sectionBits.mask.begin());
614             copy(hidlSectionBits.mode.begin(), hidlSectionBits.mode.end(),
615                     sectionBits.mode.begin());
616             aidlSection.condition.set<TunerFilterSectionCondition::sectionBits>(sectionBits);
617             break;
618         }
619         case DemuxFilterSectionSettings::Condition::hidl_discriminator::tableInfo: {
620             TunerFilterSectionTableInfo tableInfo{
621                 .tableId = static_cast<char16_t>(hidlSection.condition.tableInfo().tableId),
622                 .version = static_cast<char16_t>(hidlSection.condition.tableInfo().version),
623             };
624             aidlSection.condition.set<TunerFilterSectionCondition::tableInfo>(tableInfo);
625             break;
626         }
627     }
628     aidlSection.isCheckCrc = hidlSection.isCheckCrc;
629     aidlSection.isRepeat = hidlSection.isRepeat;
630     aidlSection.isRaw = hidlSection.isRaw;
631     return aidlSection;
632 }
633 
getAidlPesDataSettings(DemuxFilterPesDataSettings hidlPesData)634 TunerFilterPesDataSettings FilterClient::getAidlPesDataSettings(
635         DemuxFilterPesDataSettings hidlPesData) {
636     TunerFilterPesDataSettings aidlPesData{
637         .streamId = static_cast<char16_t>(hidlPesData.streamId),
638         .isRaw = hidlPesData.isRaw,
639     };
640     return aidlPesData;
641 }
642 
getAidlRecordSettings(DemuxFilterRecordSettings hidlRecord)643 TunerFilterRecordSettings FilterClient::getAidlRecordSettings(
644         DemuxFilterRecordSettings hidlRecord) {
645     TunerFilterScIndexMask mask;
646     switch (hidlRecord.scIndexMask.getDiscriminator()) {
647         case DemuxFilterRecordSettings::ScIndexMask::hidl_discriminator::sc: {
648             mask.set<TunerFilterScIndexMask::sc>(hidlRecord.scIndexMask.sc());
649             break;
650         }
651         case DemuxFilterRecordSettings::ScIndexMask::hidl_discriminator::scHevc: {
652             mask.set<TunerFilterScIndexMask::scHevc>(hidlRecord.scIndexMask.scHevc());
653             break;
654         }
655         default:
656             break;
657     }
658     TunerFilterRecordSettings aidlRecord{
659         .tsIndexMask = static_cast<int32_t>(hidlRecord.tsIndexMask),
660         .scIndexType = static_cast<int32_t>(hidlRecord.scIndexType),
661         .scIndexMask = mask,
662     };
663     return aidlRecord;
664 }
665 
getAidlDownloadSettings(DemuxFilterDownloadSettings hidlDownload)666 TunerFilterDownloadSettings FilterClient::getAidlDownloadSettings(
667         DemuxFilterDownloadSettings hidlDownload) {
668     TunerFilterDownloadSettings aidlDownload{
669         .downloadId = static_cast<int32_t>(hidlDownload.downloadId),
670     };
671     return aidlDownload;
672 }
673 
getHidlFilterEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event,DemuxFilterEventExt & eventExt)674 void TunerFilterCallback::getHidlFilterEvent(const vector<TunerFilterEvent>& filterEvents,
675         DemuxFilterEvent& event, DemuxFilterEventExt& eventExt) {
676     switch (filterEvents[0].getTag()) {
677         case  TunerFilterEvent::media: {
678             getHidlMediaEvent(filterEvents, event);
679             break;
680         }
681         case  TunerFilterEvent::section: {
682             getHidlSectionEvent(filterEvents, event);
683             break;
684         }
685         case  TunerFilterEvent::pes: {
686             getHidlPesEvent(filterEvents, event);
687             break;
688         }
689         case  TunerFilterEvent::tsRecord: {
690             getHidlTsRecordEvent(filterEvents, event, eventExt);
691             break;
692         }
693         case  TunerFilterEvent::mmtpRecord: {
694             getHidlMmtpRecordEvent(filterEvents, event, eventExt);
695             break;
696         }
697         case  TunerFilterEvent::download: {
698             getHidlDownloadEvent(filterEvents, event);
699             break;
700         }
701         case  TunerFilterEvent::ipPayload: {
702             getHidlIpPayloadEvent(filterEvents, event);
703             break;
704         }
705         case  TunerFilterEvent::temi: {
706             getHidlTemiEvent(filterEvents, event);
707             break;
708         }
709         case  TunerFilterEvent::monitor: {
710             getHidlMonitorEvent(filterEvents, eventExt);
711             break;
712         }
713         case  TunerFilterEvent::startId: {
714             getHidlRestartEvent(filterEvents, eventExt);
715             break;
716         }
717     }
718 }
719 
getHidlMediaEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event)720 void TunerFilterCallback::getHidlMediaEvent(
721         const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event) {
722     event.events.resize(filterEvents.size());
723     for (int i = 0; i < filterEvents.size(); i++) {
724         hidl_handle handle = hidl_handle(makeFromAidl(filterEvents[i]
725                 .get<TunerFilterEvent::media>().avMemory));
726         event.events[i].media({
727             .avMemory = handle,
728             .streamId = static_cast<DemuxStreamId>(filterEvents[i]
729                     .get<TunerFilterEvent::media>().streamId),
730             .isPtsPresent = filterEvents[i]
731                     .get<TunerFilterEvent::media>().isPtsPresent,
732             .pts = static_cast<uint64_t>(filterEvents[i]
733                     .get<TunerFilterEvent::media>().pts),
734             .dataLength = static_cast<uint32_t>(filterEvents[i]
735                     .get<TunerFilterEvent::media>().dataLength),
736             .offset = static_cast<uint32_t>(filterEvents[i]
737                     .get<TunerFilterEvent::media>().offset),
738             .isSecureMemory = filterEvents[i]
739                     .get<TunerFilterEvent::media>().isSecureMemory,
740             .avDataId = static_cast<uint64_t>(filterEvents[i]
741                     .get<TunerFilterEvent::media>().avDataId),
742             .mpuSequenceNumber = static_cast<uint32_t>(filterEvents[i]
743                     .get<TunerFilterEvent::media>().offset),
744             .isPesPrivateData = filterEvents[i]
745                     .get<TunerFilterEvent::media>().isPesPrivateData,
746         });
747 
748         if (filterEvents[i].get<TunerFilterEvent::media>().isAudioExtraMetaData) {
749             event.events[i].media().extraMetaData.audio({
750                 .adFade = static_cast<uint8_t>(filterEvents[i]
751                         .get<TunerFilterEvent::media>().audio.adFade),
752                 .adPan = static_cast<uint8_t>(filterEvents[i]
753                         .get<TunerFilterEvent::media>().audio.adPan),
754                 .versionTextTag = static_cast<uint8_t>(filterEvents[i]
755                         .get<TunerFilterEvent::media>().audio.versionTextTag),
756                 .adGainCenter = static_cast<uint8_t>(filterEvents[i]
757                         .get<TunerFilterEvent::media>().audio.adGainCenter),
758                 .adGainFront = static_cast<uint8_t>(filterEvents[i]
759                         .get<TunerFilterEvent::media>().audio.adGainFront),
760                 .adGainSurround = static_cast<uint8_t>(filterEvents[i]
761                         .get<TunerFilterEvent::media>().audio.adGainSurround),
762             });
763         } else {
764             event.events[i].media().extraMetaData.noinit();
765         }
766     }
767 }
768 
getHidlSectionEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event)769 void TunerFilterCallback::getHidlSectionEvent(
770         const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event) {
771     event.events.resize(filterEvents.size());
772     for (int i = 0; i < filterEvents.size(); i++) {
773         auto section = filterEvents[i].get<TunerFilterEvent::section>();
774         event.events[i].section({
775             .tableId = static_cast<uint16_t>(section.tableId),
776             .version = static_cast<uint16_t>(section.version),
777             .sectionNum = static_cast<uint16_t>(section.sectionNum),
778             .dataLength = static_cast<uint16_t>(section.dataLength),
779         });
780     }
781 }
782 
getHidlPesEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event)783 void TunerFilterCallback::getHidlPesEvent(
784         const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event) {
785     event.events.resize(filterEvents.size());
786     for (int i = 0; i < filterEvents.size(); i++) {
787         auto pes = filterEvents[i].get<TunerFilterEvent::pes>();
788         event.events[i].pes({
789             .streamId = static_cast<DemuxStreamId>(pes.streamId),
790             .dataLength = static_cast<uint16_t>(pes.dataLength),
791             .mpuSequenceNumber = static_cast<uint32_t>(pes.mpuSequenceNumber),
792         });
793     }
794 }
795 
getHidlTsRecordEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event,DemuxFilterEventExt & eventExt)796 void TunerFilterCallback::getHidlTsRecordEvent(const vector<TunerFilterEvent>& filterEvents,
797         DemuxFilterEvent& event, DemuxFilterEventExt& eventExt) {
798     event.events.resize(filterEvents.size());
799     eventExt.events.resize(filterEvents.size());
800     for (int i = 0; i < filterEvents.size(); i++) {
801         auto ts = filterEvents[i].get<TunerFilterEvent::tsRecord>();
802         event.events[i].tsRecord({
803             .tsIndexMask = static_cast<uint32_t>(ts.tsIndexMask),
804             .byteNumber = static_cast<uint64_t>(ts.byteNumber),
805         });
806         event.events[i].tsRecord().pid.tPid(static_cast<DemuxTpid>(ts.pid));
807 
808         switch (ts.scIndexMask.getTag()) {
809             case TunerFilterScIndexMask::sc: {
810                 event.events[i].tsRecord().scIndexMask.sc(
811                         ts.scIndexMask.get<TunerFilterScIndexMask::sc>());
812                 break;
813             }
814             case TunerFilterScIndexMask::scHevc: {
815                 event.events[i].tsRecord().scIndexMask.scHevc(
816                         ts.scIndexMask.get<TunerFilterScIndexMask::scHevc>());
817                 break;
818             }
819             default:
820                 break;
821         }
822 
823         if (ts.isExtended) {
824             eventExt.events[i].tsRecord({
825                 .pts = static_cast<uint64_t>(ts.pts),
826                 .firstMbInSlice = static_cast<uint32_t>(ts.firstMbInSlice),
827             });
828         } else {
829             eventExt.events[i].noinit();
830         }
831     }
832 }
833 
getHidlMmtpRecordEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event,DemuxFilterEventExt & eventExt)834 void TunerFilterCallback::getHidlMmtpRecordEvent(const vector<TunerFilterEvent>& filterEvents,
835         DemuxFilterEvent& event, DemuxFilterEventExt& eventExt) {
836     event.events.resize(filterEvents.size());
837     eventExt.events.resize(filterEvents.size());
838     for (int i = 0; i < filterEvents.size(); i++) {
839         auto mmtp = filterEvents[i].get<TunerFilterEvent::mmtpRecord>();
840         event.events[i].mmtpRecord({
841             .scHevcIndexMask = static_cast<uint32_t>(mmtp.scHevcIndexMask),
842             .byteNumber = static_cast<uint64_t>(mmtp.byteNumber),
843         });
844 
845         if (mmtp.isExtended) {
846             eventExt.events[i].mmtpRecord({
847                 .pts = static_cast<uint64_t>(mmtp.pts),
848                 .mpuSequenceNumber = static_cast<uint32_t>(mmtp.mpuSequenceNumber),
849                 .firstMbInSlice = static_cast<uint32_t>(mmtp.firstMbInSlice),
850                 .tsIndexMask = static_cast<uint32_t>(mmtp.tsIndexMask),
851             });
852         } else {
853             eventExt.events[i].noinit();
854         }
855     }
856 }
857 
getHidlDownloadEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event)858 void TunerFilterCallback::getHidlDownloadEvent(const vector<TunerFilterEvent>& filterEvents,
859         DemuxFilterEvent& event) {
860     event.events.resize(filterEvents.size());
861     for (int i = 0; i < filterEvents.size(); i++) {
862         auto download = filterEvents[i].get<TunerFilterEvent::download>();
863         event.events[i].download({
864             .itemId = static_cast<uint32_t>(download.itemId),
865             .mpuSequenceNumber = static_cast<uint32_t>(download.mpuSequenceNumber),
866             .itemFragmentIndex = static_cast<uint32_t>(download.itemFragmentIndex),
867             .lastItemFragmentIndex = static_cast<uint32_t>(download.lastItemFragmentIndex),
868             .dataLength = static_cast<uint16_t>(download.dataLength),
869         });
870     }
871 }
872 
getHidlIpPayloadEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event)873 void TunerFilterCallback::getHidlIpPayloadEvent(const vector<TunerFilterEvent>& filterEvents,
874         DemuxFilterEvent& event) {
875     event.events.resize(filterEvents.size());
876     for (int i = 0; i < filterEvents.size(); i++) {
877         auto ip = filterEvents[i].get<TunerFilterEvent::ipPayload>();
878         event.events[i].ipPayload({
879             .dataLength = static_cast<uint16_t>(ip.dataLength),
880         });
881     }
882 }
883 
getHidlTemiEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEvent & event)884 void TunerFilterCallback::getHidlTemiEvent(const vector<TunerFilterEvent>& filterEvents,
885         DemuxFilterEvent& event) {
886     event.events.resize(filterEvents.size());
887     for (int i = 0; i < filterEvents.size(); i++) {
888         auto temi = filterEvents[i].get<TunerFilterEvent::temi>();
889         event.events[i].temi({
890             .pts = static_cast<uint64_t>(temi.pts),
891             .descrTag = static_cast<uint8_t>(temi.descrTag),
892         });
893         hidl_vec<uint8_t> descrData(temi.descrData.begin(), temi.descrData.end());
894         event.events[i].temi().descrData = descrData;
895     }
896 }
897 
getHidlMonitorEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEventExt & eventExt)898 void TunerFilterCallback::getHidlMonitorEvent(const vector<TunerFilterEvent>& filterEvents,
899         DemuxFilterEventExt& eventExt) {
900     auto monitor = filterEvents[0].get<TunerFilterEvent::monitor>();
901     eventExt.events.resize(1);
902     DemuxFilterMonitorEvent monitorEvent;
903     switch (monitor.getTag()) {
904         case TunerFilterMonitorEvent::scramblingStatus: {
905             monitorEvent.scramblingStatus(static_cast<ScramblingStatus>(monitor.scramblingStatus));
906             eventExt.events[0].monitorEvent(monitorEvent);
907             break;
908         }
909         case TunerFilterMonitorEvent::cid: {
910             monitorEvent.cid(static_cast<uint32_t>(monitor.cid));
911             eventExt.events[0].monitorEvent(monitorEvent);
912             break;
913         }
914     }
915 }
916 
getHidlRestartEvent(const vector<TunerFilterEvent> & filterEvents,DemuxFilterEventExt & eventExt)917 void TunerFilterCallback::getHidlRestartEvent(const vector<TunerFilterEvent>& filterEvents,
918         DemuxFilterEventExt& eventExt) {
919     uint32_t startId = filterEvents[0].get<TunerFilterEvent::startId>();
920     eventExt.events.resize(1);
921     eventExt.events[0].startId(static_cast<uint32_t>(startId));
922 }
923 
getFilterMq()924 Result FilterClient::getFilterMq() {
925     if (mFilterMQ != NULL) {
926         return Result::SUCCESS;
927     }
928 
929     AidlMQDesc aidlMqDesc;
930     Result res = Result::UNAVAILABLE;
931 
932     if (mTunerFilter != NULL) {
933         Status s = mTunerFilter->getQueueDesc(&aidlMqDesc);
934         res = ClientHelper::getServiceSpecificErrorCode(s);
935         if (res == Result::SUCCESS) {
936             mFilterMQ = new (nothrow) AidlMQ(aidlMqDesc, false/*resetPointer*/);
937             EventFlag::createEventFlag(mFilterMQ->getEventFlagWord(), &mFilterMQEventFlag);
938         }
939         return res;
940     }
941 
942     if (mFilter != NULL) {
943         MQDescriptorSync<uint8_t> filterMQDesc;
944         mFilter->getQueueDesc(
945                 [&](Result r, const MQDescriptorSync<uint8_t>& desc) {
946                     filterMQDesc = desc;
947                     res = r;
948                 });
949         if (res == Result::SUCCESS) {
950             AidlMQDesc aidlMQDesc;
951             unsafeHidlToAidlMQDescriptor<uint8_t, int8_t, SynchronizedReadWrite>(
952                     filterMQDesc,  &aidlMQDesc);
953             mFilterMQ = new (nothrow) AidlMessageQueue(aidlMQDesc, false/*resetPointer*/);
954             EventFlag::createEventFlag(mFilterMQ->getEventFlagWord(), &mFilterMQEventFlag);
955         }
956     }
957 
958     return res;
959 }
960 
copyData(int8_t * buffer,int size)961 int FilterClient::copyData(int8_t* buffer, int size) {
962     if (mFilterMQ == NULL || mFilterMQEventFlag == NULL) {
963         return -1;
964     }
965 
966     int available = mFilterMQ->availableToRead();
967     size = min(size, available);
968 
969     if (mFilterMQ->read(buffer, size)) {
970         mFilterMQEventFlag->wake(static_cast<uint32_t>(DemuxQueueNotifyBits::DATA_CONSUMED));
971     } else {
972         return -1;
973     }
974 
975     return size;
976 }
977 
checkIsMediaFilter(DemuxFilterType type)978 void FilterClient::checkIsMediaFilter(DemuxFilterType type) {
979     if (type.mainType == DemuxFilterMainType::MMTP) {
980         if (type.subType.mmtpFilterType() == DemuxMmtpFilterType::AUDIO ||
981                 type.subType.mmtpFilterType() == DemuxMmtpFilterType::VIDEO) {
982             mIsMediaFilter = true;
983             return;
984         }
985     }
986 
987     if (type.mainType == DemuxFilterMainType::TS) {
988         if (type.subType.tsFilterType() == DemuxTsFilterType::AUDIO ||
989                 type.subType.tsFilterType() == DemuxTsFilterType::VIDEO) {
990             mIsMediaFilter = true;
991             return;
992         }
993     }
994 
995     mIsMediaFilter = false;
996 }
997 
checkIsPassthroughFilter(DemuxFilterSettings configure)998 void FilterClient::checkIsPassthroughFilter(DemuxFilterSettings configure) {
999     if (!mIsMediaFilter) {
1000         mIsPassthroughFilter = false;
1001         return;
1002     }
1003 
1004     if (configure.getDiscriminator() == DemuxFilterSettings::hidl_discriminator::ts) {
1005         if (configure.ts().filterSettings.av().isPassthrough) {
1006             mIsPassthroughFilter = true;
1007             return;
1008         }
1009     }
1010 
1011     if (configure.getDiscriminator() == DemuxFilterSettings::hidl_discriminator::mmtp) {
1012         if (configure.mmtp().filterSettings.av().isPassthrough) {
1013             mIsPassthroughFilter = true;
1014             return;
1015         }
1016     }
1017 
1018     mIsPassthroughFilter = false;
1019 }
1020 
handleAvShareMemory()1021 void FilterClient::handleAvShareMemory() {
1022     if (mAvSharedHandle != NULL) {
1023         return;
1024     }
1025     if (mTunerFilter != NULL && mIsMediaFilter && !mIsPassthroughFilter) {
1026         TunerFilterSharedHandleInfo aidlHandleInfo;
1027         Status s = mTunerFilter->getAvSharedHandleInfo(&aidlHandleInfo);
1028         if (ClientHelper::getServiceSpecificErrorCode(s) == Result::SUCCESS) {
1029             mAvSharedHandle = native_handle_clone(makeFromAidl(aidlHandleInfo.handle));
1030             mAvSharedMemSize = aidlHandleInfo.size;
1031         }
1032         return;
1033     }
1034 
1035     if (mFilter_1_1 != NULL && mIsMediaFilter && !mIsPassthroughFilter) {
1036         mFilter_1_1->getAvSharedHandle([&](Result r, hidl_handle avMemory, uint64_t avMemSize) {
1037             if (r == Result::SUCCESS) {
1038                 mAvSharedHandle = native_handle_clone(avMemory.getNativeHandle());
1039                 mAvSharedMemSize = avMemSize;
1040             }
1041         });
1042     }
1043 }
1044 
closeAvSharedMemory()1045 void FilterClient::closeAvSharedMemory() {
1046     if (mAvSharedHandle == NULL) {
1047         mAvSharedMemSize = 0;
1048         return;
1049     }
1050     native_handle_close(mAvSharedHandle);
1051     native_handle_delete(mAvSharedHandle);
1052     mAvSharedMemSize = 0;
1053     mAvSharedHandle = NULL;
1054 }
1055 }  // namespace android
1056