• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "TunerHidlFilter"
18 
19 #include "TunerHidlFilter.h"
20 
21 #include <aidl/android/hardware/tv/tuner/Constant.h>
22 #include <aidl/android/hardware/tv/tuner/DemuxScIndex.h>
23 #include <aidl/android/hardware/tv/tuner/Result.h>
24 #include <aidlcommonsupport/NativeHandle.h>
25 #include <binder/IPCThreadState.h>
26 #include <fmq/ConvertMQDescriptors.h>
27 
28 #include "TunerHelper.h"
29 #include "TunerHidlService.h"
30 
31 using ::aidl::android::hardware::tv::tuner::AudioExtraMetaData;
32 using ::aidl::android::hardware::tv::tuner::AudioStreamType;
33 using ::aidl::android::hardware::tv::tuner::Constant;
34 using ::aidl::android::hardware::tv::tuner::DemuxAlpFilterSettings;
35 using ::aidl::android::hardware::tv::tuner::DemuxAlpFilterSettingsFilterSettings;
36 using ::aidl::android::hardware::tv::tuner::DemuxFilterDownloadEvent;
37 using ::aidl::android::hardware::tv::tuner::DemuxFilterIpPayloadEvent;
38 using ::aidl::android::hardware::tv::tuner::DemuxFilterMainType;
39 using ::aidl::android::hardware::tv::tuner::DemuxFilterMediaEvent;
40 using ::aidl::android::hardware::tv::tuner::DemuxFilterMediaEventExtraMetaData;
41 using ::aidl::android::hardware::tv::tuner::DemuxFilterMmtpRecordEvent;
42 using ::aidl::android::hardware::tv::tuner::DemuxFilterMonitorEvent;
43 using ::aidl::android::hardware::tv::tuner::DemuxFilterPesEvent;
44 using ::aidl::android::hardware::tv::tuner::DemuxFilterScIndexMask;
45 using ::aidl::android::hardware::tv::tuner::DemuxFilterSectionBits;
46 using ::aidl::android::hardware::tv::tuner::DemuxFilterSectionEvent;
47 using ::aidl::android::hardware::tv::tuner::DemuxFilterSectionSettingsCondition;
48 using ::aidl::android::hardware::tv::tuner::DemuxFilterSectionSettingsConditionTableInfo;
49 using ::aidl::android::hardware::tv::tuner::DemuxFilterSubType;
50 using ::aidl::android::hardware::tv::tuner::DemuxFilterTemiEvent;
51 using ::aidl::android::hardware::tv::tuner::DemuxFilterTsRecordEvent;
52 using ::aidl::android::hardware::tv::tuner::DemuxIpAddress;
53 using ::aidl::android::hardware::tv::tuner::DemuxIpAddressIpAddress;
54 using ::aidl::android::hardware::tv::tuner::DemuxIpFilterSettings;
55 using ::aidl::android::hardware::tv::tuner::DemuxIpFilterSettingsFilterSettings;
56 using ::aidl::android::hardware::tv::tuner::DemuxMmtpFilterSettings;
57 using ::aidl::android::hardware::tv::tuner::DemuxMmtpFilterSettingsFilterSettings;
58 using ::aidl::android::hardware::tv::tuner::DemuxMmtpFilterType;
59 using ::aidl::android::hardware::tv::tuner::DemuxPid;
60 using ::aidl::android::hardware::tv::tuner::DemuxScIndex;
61 using ::aidl::android::hardware::tv::tuner::DemuxTlvFilterSettings;
62 using ::aidl::android::hardware::tv::tuner::DemuxTlvFilterSettingsFilterSettings;
63 using ::aidl::android::hardware::tv::tuner::DemuxTsFilterSettings;
64 using ::aidl::android::hardware::tv::tuner::DemuxTsFilterSettingsFilterSettings;
65 using ::aidl::android::hardware::tv::tuner::DemuxTsFilterType;
66 using ::aidl::android::hardware::tv::tuner::Result;
67 using ::aidl::android::hardware::tv::tuner::ScramblingStatus;
68 using ::android::dupToAidl;
69 using ::android::IPCThreadState;
70 using ::android::makeFromAidl;
71 using ::android::unsafeHidlToAidlMQDescriptor;
72 using ::android::hardware::hidl_handle;
73 
74 using HidlDemuxAlpLengthType = ::android::hardware::tv::tuner::V1_0::DemuxAlpLengthType;
75 using HidlDemuxFilterMainType = ::android::hardware::tv::tuner::V1_0::DemuxFilterMainType;
76 using HidlDemuxIpAddress = ::android::hardware::tv::tuner::V1_0::DemuxIpAddress;
77 using HidlDemuxMmtpFilterType = ::android::hardware::tv::tuner::V1_0::DemuxMmtpFilterType;
78 using HidlDemuxMmtpPid = ::android::hardware::tv::tuner::V1_0::DemuxMmtpPid;
79 using HidlDemuxRecordScIndexType = ::android::hardware::tv::tuner::V1_0::DemuxRecordScIndexType;
80 using HidlDemuxStreamId = ::android::hardware::tv::tuner::V1_0::DemuxStreamId;
81 using HidlDemuxTsFilterType = ::android::hardware::tv::tuner::V1_0::DemuxTsFilterType;
82 using HidlResult = ::android::hardware::tv::tuner::V1_0::Result;
83 using HidlAudioStreamType = ::android::hardware::tv::tuner::V1_1::AudioStreamType;
84 using HidlConstant = ::android::hardware::tv::tuner::V1_1::Constant;
85 using HidlVideoStreamType = ::android::hardware::tv::tuner::V1_1::VideoStreamType;
86 
87 using namespace std;
88 
89 namespace aidl {
90 namespace android {
91 namespace media {
92 namespace tv {
93 namespace tuner {
94 
TunerHidlFilter(const sp<HidlIFilter> filter,const sp<FilterCallback> cb,const DemuxFilterType type,const shared_ptr<TunerHidlService> tuner)95 TunerHidlFilter::TunerHidlFilter(const sp<HidlIFilter> filter, const sp<FilterCallback> cb,
96                                  const DemuxFilterType type,
97                                  const shared_ptr<TunerHidlService> tuner)
98       : mFilter(filter),
99         mType(type),
100         mStarted(false),
101         mShared(false),
102         mClientPid(-1),
103         mFilterCallback(cb),
104         mTunerService(tuner) {
105     mFilter_1_1 = ::android::hardware::tv::tuner::V1_1::IFilter::castFrom(filter);
106 }
107 
~TunerHidlFilter()108 TunerHidlFilter::~TunerHidlFilter() {
109     freeSharedFilterToken("");
110 
111     {
112         Mutex::Autolock _l(mLock);
113         mFilter = nullptr;
114         mFilter_1_1 = nullptr;
115         mTunerService = nullptr;
116     }
117 }
118 
getQueueDesc(AidlMQDesc * _aidl_return)119 ::ndk::ScopedAStatus TunerHidlFilter::getQueueDesc(AidlMQDesc* _aidl_return) {
120     Mutex::Autolock _l(mLock);
121     if (mShared) {
122         IPCThreadState* ipc = IPCThreadState::self();
123         int32_t callingPid = ipc->getCallingPid();
124         if (callingPid == mClientPid) {
125             ALOGD("%s is called in wrong process", __FUNCTION__);
126             return ::ndk::ScopedAStatus::fromServiceSpecificError(
127                     static_cast<int32_t>(Result::INVALID_STATE));
128         }
129     }
130 
131     MQDesc filterMQDesc;
132     HidlResult res;
133     mFilter->getQueueDesc([&](HidlResult r, const MQDesc& desc) {
134         filterMQDesc = desc;
135         res = r;
136     });
137     if (res != HidlResult::SUCCESS) {
138         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
139     }
140 
141     AidlMQDesc aidlMQDesc;
142     unsafeHidlToAidlMQDescriptor<uint8_t, int8_t, SynchronizedReadWrite>(filterMQDesc, &aidlMQDesc);
143     *_aidl_return = std::move(aidlMQDesc);
144 
145     return ::ndk::ScopedAStatus::ok();
146 }
147 
getId(int32_t * _aidl_return)148 ::ndk::ScopedAStatus TunerHidlFilter::getId(int32_t* _aidl_return) {
149     Mutex::Autolock _l(mLock);
150     if (mShared) {
151         ALOGD("%s is called on a shared filter", __FUNCTION__);
152         return ::ndk::ScopedAStatus::fromServiceSpecificError(
153                 static_cast<int32_t>(Result::INVALID_STATE));
154     }
155 
156     HidlResult res;
157     mFilter->getId([&](HidlResult r, uint32_t filterId) {
158         res = r;
159         mId = filterId;
160     });
161     if (res != HidlResult::SUCCESS) {
162         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
163     }
164     *_aidl_return = mId;
165 
166     return ::ndk::ScopedAStatus::ok();
167 }
168 
getId64Bit(int64_t * _aidl_return)169 ::ndk::ScopedAStatus TunerHidlFilter::getId64Bit(int64_t* _aidl_return) {
170     Mutex::Autolock _l(mLock);
171     if (mFilter_1_1 == nullptr) {
172         ALOGE("IFilter_1_1 is not initialized");
173         return ::ndk::ScopedAStatus::fromServiceSpecificError(
174                 static_cast<int32_t>(Result::UNAVAILABLE));
175     }
176 
177     if (mShared) {
178         ALOGD("%s is called on a shared filter", __FUNCTION__);
179         return ::ndk::ScopedAStatus::fromServiceSpecificError(
180                 static_cast<int32_t>(Result::INVALID_STATE));
181     }
182 
183     HidlResult res;
184     mFilter_1_1->getId64Bit([&](HidlResult r, uint64_t filterId) {
185         res = r;
186         mId64Bit = filterId;
187     });
188     if (res != HidlResult::SUCCESS) {
189         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
190     }
191     *_aidl_return = mId64Bit;
192 
193     return ::ndk::ScopedAStatus::ok();
194 }
195 
configure(const DemuxFilterSettings & in_settings)196 ::ndk::ScopedAStatus TunerHidlFilter::configure(const DemuxFilterSettings& in_settings) {
197     Mutex::Autolock _l(mLock);
198     if (mShared) {
199         ALOGD("%s is called on a shared filter", __FUNCTION__);
200         return ::ndk::ScopedAStatus::fromServiceSpecificError(
201                 static_cast<int32_t>(Result::INVALID_STATE));
202     }
203 
204     HidlDemuxFilterSettings settings;
205     switch (in_settings.getTag()) {
206     case DemuxFilterSettings::ts: {
207         getHidlTsSettings(in_settings, settings);
208         break;
209     }
210     case DemuxFilterSettings::mmtp: {
211         getHidlMmtpSettings(in_settings, settings);
212         break;
213     }
214     case DemuxFilterSettings::ip: {
215         getHidlIpSettings(in_settings, settings);
216         break;
217     }
218     case DemuxFilterSettings::tlv: {
219         getHidlTlvSettings(in_settings, settings);
220         break;
221     }
222     case DemuxFilterSettings::alp: {
223         getHidlAlpSettings(in_settings, settings);
224         break;
225     }
226     }
227 
228     HidlResult res = mFilter->configure(settings);
229     if (res != HidlResult::SUCCESS) {
230         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
231     }
232 
233     return ::ndk::ScopedAStatus::ok();
234 }
235 
configureMonitorEvent(int32_t monitorEventType)236 ::ndk::ScopedAStatus TunerHidlFilter::configureMonitorEvent(int32_t monitorEventType) {
237     Mutex::Autolock _l(mLock);
238     if (mFilter_1_1 == nullptr) {
239         ALOGE("IFilter_1_1 is not initialized");
240         return ::ndk::ScopedAStatus::fromServiceSpecificError(
241                 static_cast<int32_t>(Result::UNAVAILABLE));
242     }
243 
244     if (mShared) {
245         ALOGD("%s is called on a shared filter", __FUNCTION__);
246         return ::ndk::ScopedAStatus::fromServiceSpecificError(
247                 static_cast<int32_t>(Result::INVALID_STATE));
248     }
249 
250     HidlResult res = mFilter_1_1->configureMonitorEvent(monitorEventType);
251     if (res != HidlResult::SUCCESS) {
252         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
253     }
254 
255     return ::ndk::ScopedAStatus::ok();
256 }
257 
configureIpFilterContextId(int32_t cid)258 ::ndk::ScopedAStatus TunerHidlFilter::configureIpFilterContextId(int32_t cid) {
259     Mutex::Autolock _l(mLock);
260     if (mFilter_1_1 == nullptr) {
261         ALOGE("IFilter_1_1 is not initialized");
262         return ::ndk::ScopedAStatus::fromServiceSpecificError(
263                 static_cast<int32_t>(Result::UNAVAILABLE));
264     }
265 
266     if (mShared) {
267         ALOGD("%s is called on a shared filter", __FUNCTION__);
268         return ::ndk::ScopedAStatus::fromServiceSpecificError(
269                 static_cast<int32_t>(Result::INVALID_STATE));
270     }
271 
272     HidlResult res = mFilter_1_1->configureIpCid(cid);
273     if (res != HidlResult::SUCCESS) {
274         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
275     }
276 
277     return ::ndk::ScopedAStatus::ok();
278 }
279 
configureAvStreamType(const AvStreamType & in_avStreamType)280 ::ndk::ScopedAStatus TunerHidlFilter::configureAvStreamType(const AvStreamType& in_avStreamType) {
281     Mutex::Autolock _l(mLock);
282     if (mFilter_1_1 == nullptr) {
283         ALOGE("IFilter_1_1 is not initialized");
284         return ::ndk::ScopedAStatus::fromServiceSpecificError(
285                 static_cast<int32_t>(Result::UNAVAILABLE));
286     }
287 
288     if (mShared) {
289         ALOGD("%s is called on a shared filter", __FUNCTION__);
290         return ::ndk::ScopedAStatus::fromServiceSpecificError(
291                 static_cast<int32_t>(Result::INVALID_STATE));
292     }
293 
294     HidlAvStreamType type;
295     if (!getHidlAvStreamType(in_avStreamType, type)) {
296         return ::ndk::ScopedAStatus::fromServiceSpecificError(
297                 static_cast<int32_t>(Result::INVALID_STATE));
298     }
299 
300     HidlResult res = mFilter_1_1->configureAvStreamType(type);
301     if (res != HidlResult::SUCCESS) {
302         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
303     }
304 
305     return ::ndk::ScopedAStatus::ok();
306 }
307 
setDataSource(const shared_ptr<ITunerFilter> & filter)308 ::ndk::ScopedAStatus TunerHidlFilter::setDataSource(const shared_ptr<ITunerFilter>& filter) {
309     Mutex::Autolock _l(mLock);
310     if (filter == nullptr) {
311         return ::ndk::ScopedAStatus::fromServiceSpecificError(
312                 static_cast<int32_t>(Result::INVALID_ARGUMENT));
313     }
314 
315     if (mShared) {
316         ALOGD("%s is called on a shared filter", __FUNCTION__);
317         return ::ndk::ScopedAStatus::fromServiceSpecificError(
318                 static_cast<int32_t>(Result::INVALID_STATE));
319     }
320 
321     sp<HidlIFilter> hidlFilter = static_cast<TunerHidlFilter*>(filter.get())->getHalFilter();
322     HidlResult res = mFilter->setDataSource(hidlFilter);
323     if (res != HidlResult::SUCCESS) {
324         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
325     }
326 
327     return ::ndk::ScopedAStatus::ok();
328 }
329 
getAvSharedHandle(NativeHandle * out_avMemory,int64_t * _aidl_return)330 ::ndk::ScopedAStatus TunerHidlFilter::getAvSharedHandle(NativeHandle* out_avMemory,
331                                                         int64_t* _aidl_return) {
332     Mutex::Autolock _l(mLock);
333     if (mFilter_1_1 == nullptr) {
334         ALOGE("IFilter_1_1 is not initialized");
335         return ::ndk::ScopedAStatus::fromServiceSpecificError(
336                 static_cast<int32_t>(Result::UNAVAILABLE));
337     }
338 
339     if (mShared) {
340         ALOGD("%s is called on a shared filter", __FUNCTION__);
341         return ::ndk::ScopedAStatus::fromServiceSpecificError(
342                 static_cast<int32_t>(Result::INVALID_STATE));
343     }
344 
345     HidlResult res;
346     mFilter_1_1->getAvSharedHandle([&](HidlResult r, hidl_handle avMemory, uint64_t avMemSize) {
347         res = r;
348         if (res == HidlResult::SUCCESS) {
349             *out_avMemory = dupToAidl(avMemory);
350             *_aidl_return = static_cast<int64_t>(avMemSize);
351         }
352     });
353 
354     if (res != HidlResult::SUCCESS) {
355         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
356     }
357 
358     // Call to HAL to make sure the transport FD was able to be closed by binder.
359     // This is a tricky workaround for a problem in Binder.
360     // TODO:[b/192048842] When that problem is fixed we may be able to remove or change this code.
361     mFilter_1_1->getId([&](HidlResult /* r */, uint32_t /* filterId*/){});
362 
363     return ::ndk::ScopedAStatus::ok();
364 }
365 
releaseAvHandle(const NativeHandle & in_handle,int64_t in_avDataId)366 ::ndk::ScopedAStatus TunerHidlFilter::releaseAvHandle(const NativeHandle& in_handle,
367                                                       int64_t in_avDataId) {
368     Mutex::Autolock _l(mLock);
369     if (mShared) {
370         ALOGD("%s is called on a shared filter", __FUNCTION__);
371         return ::ndk::ScopedAStatus::fromServiceSpecificError(
372                 static_cast<int32_t>(Result::INVALID_STATE));
373     }
374 
375     hidl_handle handle;
376     handle.setTo(makeFromAidl(in_handle));
377     HidlResult res = mFilter->releaseAvHandle(handle, in_avDataId);
378     if (res != HidlResult::SUCCESS) {
379         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
380     }
381 
382     // Call to HAL to make sure the transport FD was able to be closed by binder.
383     // This is a tricky workaround for a problem in Binder.
384     // TODO:[b/192048842] When that problem is fixed we may be able to remove or change this code.
385     mFilter->getId([&](HidlResult /* r */, uint32_t /* filterId*/){});
386 
387     return ::ndk::ScopedAStatus::ok();
388 }
389 
start()390 ::ndk::ScopedAStatus TunerHidlFilter::start() {
391     Mutex::Autolock _l(mLock);
392     if (mShared) {
393         IPCThreadState* ipc = IPCThreadState::self();
394         int32_t callingPid = ipc->getCallingPid();
395         if (callingPid == mClientPid) {
396             ALOGD("%s is called in wrong process", __FUNCTION__);
397             return ::ndk::ScopedAStatus::fromServiceSpecificError(
398                     static_cast<int32_t>(Result::INVALID_STATE));
399         }
400     }
401 
402     HidlResult res = mFilter->start();
403     if (res != HidlResult::SUCCESS) {
404         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
405     }
406     mStarted = true;
407 
408     return ::ndk::ScopedAStatus::ok();
409 }
410 
stop()411 ::ndk::ScopedAStatus TunerHidlFilter::stop() {
412     Mutex::Autolock _l(mLock);
413     if (mShared) {
414         IPCThreadState* ipc = IPCThreadState::self();
415         int32_t callingPid = ipc->getCallingPid();
416         if (callingPid == mClientPid) {
417             ALOGD("%s is called in wrong process", __FUNCTION__);
418             return ::ndk::ScopedAStatus::fromServiceSpecificError(
419                     static_cast<int32_t>(Result::INVALID_STATE));
420         }
421     }
422 
423     HidlResult res = mFilter->stop();
424     mStarted = false;
425     if (res != HidlResult::SUCCESS) {
426         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
427     }
428 
429     return ::ndk::ScopedAStatus::ok();
430 }
431 
flush()432 ::ndk::ScopedAStatus TunerHidlFilter::flush() {
433     Mutex::Autolock _l(mLock);
434     if (mShared) {
435         IPCThreadState* ipc = IPCThreadState::self();
436         int32_t callingPid = ipc->getCallingPid();
437         if (callingPid == mClientPid) {
438             ALOGD("%s is called in wrong process", __FUNCTION__);
439             return ::ndk::ScopedAStatus::fromServiceSpecificError(
440                     static_cast<int32_t>(Result::INVALID_STATE));
441         }
442     }
443 
444     HidlResult res = mFilter->flush();
445     if (res != HidlResult::SUCCESS) {
446         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
447     }
448 
449     return ::ndk::ScopedAStatus::ok();
450 }
451 
close()452 ::ndk::ScopedAStatus TunerHidlFilter::close() {
453     Mutex::Autolock _l(mLock);
454     if (mShared) {
455         IPCThreadState* ipc = IPCThreadState::self();
456         int32_t callingPid = ipc->getCallingPid();
457         if (callingPid == mClientPid) {
458             if (mFilterCallback != nullptr) {
459                 mFilterCallback->sendSharedFilterStatus(STATUS_INACCESSIBLE);
460                 mFilterCallback->detachSharedFilterCallback();
461             }
462             mTunerService->removeSharedFilter(this->ref<TunerHidlFilter>());
463         } else {
464             // Calling from shared process, do not really close this filter.
465             if (mFilterCallback != nullptr) {
466                 mFilterCallback->detachSharedFilterCallback();
467             }
468             mStarted = false;
469             return ::ndk::ScopedAStatus::ok();
470         }
471     }
472 
473     if (mFilterCallback != nullptr) {
474         mFilterCallback->detachCallbacks();
475     }
476     HidlResult res = mFilter->close();
477     mStarted = false;
478     mShared = false;
479     mClientPid = -1;
480 
481     if (res != HidlResult::SUCCESS) {
482         return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
483     }
484 
485     return ::ndk::ScopedAStatus::ok();
486 }
487 
acquireSharedFilterToken(string * _aidl_return)488 ::ndk::ScopedAStatus TunerHidlFilter::acquireSharedFilterToken(string* _aidl_return) {
489     Mutex::Autolock _l(mLock);
490     if (mShared || mStarted) {
491         ALOGD("create SharedFilter in wrong state");
492         return ::ndk::ScopedAStatus::fromServiceSpecificError(
493                 static_cast<int32_t>(Result::INVALID_STATE));
494     }
495 
496     IPCThreadState* ipc = IPCThreadState::self();
497     mClientPid = ipc->getCallingPid();
498     string token = mTunerService->addFilterToShared(this->ref<TunerHidlFilter>());
499     _aidl_return->assign(token);
500     mShared = true;
501 
502     return ::ndk::ScopedAStatus::ok();
503 }
504 
freeSharedFilterToken(const string &)505 ::ndk::ScopedAStatus TunerHidlFilter::freeSharedFilterToken(const string& /* in_filterToken */) {
506     Mutex::Autolock _l(mLock);
507     if (!mShared) {
508         // The filter is not shared or the shared filter has been closed.
509         return ::ndk::ScopedAStatus::ok();
510     }
511 
512     if (mFilterCallback != nullptr) {
513         mFilterCallback->sendSharedFilterStatus(STATUS_INACCESSIBLE);
514         mFilterCallback->detachSharedFilterCallback();
515     }
516 
517     mTunerService->removeSharedFilter(this->ref<TunerHidlFilter>());
518     mShared = false;
519 
520     return ::ndk::ScopedAStatus::ok();
521 }
522 
getFilterType(DemuxFilterType * _aidl_return)523 ::ndk::ScopedAStatus TunerHidlFilter::getFilterType(DemuxFilterType* _aidl_return) {
524     *_aidl_return = mType;
525     return ::ndk::ScopedAStatus::ok();
526 }
527 
setDelayHint(const FilterDelayHint &)528 ::ndk::ScopedAStatus TunerHidlFilter::setDelayHint(const FilterDelayHint&) {
529     // setDelayHint is not supported in HIDL HAL
530     return ::ndk::ScopedAStatus::fromServiceSpecificError(
531                 static_cast<int32_t>(Result::UNAVAILABLE));
532 }
533 
isSharedFilterAllowed(int callingPid)534 bool TunerHidlFilter::isSharedFilterAllowed(int callingPid) {
535     return mShared && mClientPid != callingPid;
536 }
537 
attachSharedFilterCallback(const shared_ptr<ITunerFilterCallback> & in_cb)538 void TunerHidlFilter::attachSharedFilterCallback(const shared_ptr<ITunerFilterCallback>& in_cb) {
539     if (mFilterCallback != nullptr) {
540         mFilterCallback->attachSharedFilterCallback(in_cb);
541     }
542 }
543 
getHalFilter()544 sp<HidlIFilter> TunerHidlFilter::getHalFilter() {
545     return mFilter;
546 }
547 
getHidlAvStreamType(const AvStreamType avStreamType,HidlAvStreamType & type)548 bool TunerHidlFilter::getHidlAvStreamType(const AvStreamType avStreamType, HidlAvStreamType& type) {
549     if (isAudioFilter()) {
550         AudioStreamType audio = avStreamType.get<AvStreamType::audio>();
551         if (static_cast<int32_t>(audio) > static_cast<int32_t>(HidlAudioStreamType::DRA)) {
552             return false;
553         }
554         type.audio(static_cast<HidlAudioStreamType>(audio));
555         return true;
556     }
557 
558     if (isVideoFilter()) {
559         type.video(static_cast<HidlVideoStreamType>(avStreamType.get<AvStreamType::video>()));
560         return true;
561     }
562 
563     return false;
564 }
565 
isAudioFilter()566 bool TunerHidlFilter::isAudioFilter() {
567     return (mType.mainType == DemuxFilterMainType::TS &&
568             mType.subType.get<DemuxFilterSubType::tsFilterType>() == DemuxTsFilterType::AUDIO) ||
569            (mType.mainType == DemuxFilterMainType::MMTP &&
570             mType.subType.get<DemuxFilterSubType::mmtpFilterType>() == DemuxMmtpFilterType::AUDIO);
571 }
572 
isVideoFilter()573 bool TunerHidlFilter::isVideoFilter() {
574     return (mType.mainType == DemuxFilterMainType::TS &&
575             mType.subType.get<DemuxFilterSubType::tsFilterType>() == DemuxTsFilterType::VIDEO) ||
576            (mType.mainType == DemuxFilterMainType::MMTP &&
577             mType.subType.get<DemuxFilterSubType::mmtpFilterType>() == DemuxMmtpFilterType::VIDEO);
578 }
579 
getHidlTsSettings(const DemuxFilterSettings & settings,HidlDemuxFilterSettings & hidlSettings)580 void TunerHidlFilter::getHidlTsSettings(const DemuxFilterSettings& settings,
581                                         HidlDemuxFilterSettings& hidlSettings) {
582     const DemuxTsFilterSettings& tsConf = settings.get<DemuxFilterSettings::ts>();
583     HidlDemuxTsFilterSettings ts{
584             .tpid = static_cast<uint16_t>(tsConf.tpid),
585     };
586 
587     switch (tsConf.filterSettings.getTag()) {
588     case DemuxTsFilterSettingsFilterSettings::av: {
589         ts.filterSettings.av(getHidlAvSettings(
590                 tsConf.filterSettings.get<DemuxTsFilterSettingsFilterSettings::av>()));
591         break;
592     }
593     case DemuxTsFilterSettingsFilterSettings::section: {
594         ts.filterSettings.section(getHidlSectionSettings(
595                 tsConf.filterSettings.get<DemuxTsFilterSettingsFilterSettings::section>()));
596         break;
597     }
598     case DemuxTsFilterSettingsFilterSettings::pesData: {
599         ts.filterSettings.pesData(getHidlPesDataSettings(
600                 tsConf.filterSettings.get<DemuxTsFilterSettingsFilterSettings::pesData>()));
601         break;
602     }
603     case DemuxTsFilterSettingsFilterSettings::record: {
604         ts.filterSettings.record(getHidlRecordSettings(
605                 tsConf.filterSettings.get<DemuxTsFilterSettingsFilterSettings::record>()));
606         break;
607     }
608     default: {
609         ts.filterSettings.noinit();
610         break;
611     }
612     }
613     hidlSettings.ts(ts);
614 }
615 
getHidlMmtpSettings(const DemuxFilterSettings & settings,HidlDemuxFilterSettings & hidlSettings)616 void TunerHidlFilter::getHidlMmtpSettings(const DemuxFilterSettings& settings,
617                                           HidlDemuxFilterSettings& hidlSettings) {
618     const DemuxMmtpFilterSettings& mmtpConf = settings.get<DemuxFilterSettings::mmtp>();
619     HidlDemuxMmtpFilterSettings mmtp{
620             .mmtpPid = static_cast<HidlDemuxMmtpPid>(mmtpConf.mmtpPid),
621     };
622 
623     switch (mmtpConf.filterSettings.getTag()) {
624     case DemuxMmtpFilterSettingsFilterSettings::av: {
625         mmtp.filterSettings.av(getHidlAvSettings(
626                 mmtpConf.filterSettings.get<DemuxMmtpFilterSettingsFilterSettings::av>()));
627         break;
628     }
629     case DemuxMmtpFilterSettingsFilterSettings::section: {
630         mmtp.filterSettings.section(getHidlSectionSettings(
631                 mmtpConf.filterSettings.get<DemuxMmtpFilterSettingsFilterSettings::section>()));
632         break;
633     }
634     case DemuxMmtpFilterSettingsFilterSettings::pesData: {
635         mmtp.filterSettings.pesData(getHidlPesDataSettings(
636                 mmtpConf.filterSettings.get<DemuxMmtpFilterSettingsFilterSettings::pesData>()));
637         break;
638     }
639     case DemuxMmtpFilterSettingsFilterSettings::record: {
640         mmtp.filterSettings.record(getHidlRecordSettings(
641                 mmtpConf.filterSettings.get<DemuxMmtpFilterSettingsFilterSettings::record>()));
642         break;
643     }
644     case DemuxMmtpFilterSettingsFilterSettings::download: {
645         mmtp.filterSettings.download(getHidlDownloadSettings(
646                 mmtpConf.filterSettings.get<DemuxMmtpFilterSettingsFilterSettings::download>()));
647         break;
648     }
649     default: {
650         mmtp.filterSettings.noinit();
651         break;
652     }
653     }
654     hidlSettings.mmtp(mmtp);
655 }
656 
getHidlIpSettings(const DemuxFilterSettings & settings,HidlDemuxFilterSettings & hidlSettings)657 void TunerHidlFilter::getHidlIpSettings(const DemuxFilterSettings& settings,
658                                         HidlDemuxFilterSettings& hidlSettings) {
659     const DemuxIpFilterSettings& ipConf = settings.get<DemuxFilterSettings::ip>();
660     HidlDemuxIpAddress ipAddr{
661             .srcPort = static_cast<uint16_t>(ipConf.ipAddr.srcPort),
662             .dstPort = static_cast<uint16_t>(ipConf.ipAddr.dstPort),
663     };
664 
665     ipConf.ipAddr.srcIpAddress.getTag() == DemuxIpAddressIpAddress::v6
666             ? ipAddr.srcIpAddress.v6(getIpV6Address(ipConf.ipAddr.srcIpAddress))
667             : ipAddr.srcIpAddress.v4(getIpV4Address(ipConf.ipAddr.srcIpAddress));
668     ipConf.ipAddr.dstIpAddress.getTag() == DemuxIpAddressIpAddress::v6
669             ? ipAddr.dstIpAddress.v6(getIpV6Address(ipConf.ipAddr.dstIpAddress))
670             : ipAddr.dstIpAddress.v4(getIpV4Address(ipConf.ipAddr.dstIpAddress));
671 
672     HidlDemuxIpFilterSettings ip;
673     ip.ipAddr = ipAddr;
674 
675     switch (ipConf.filterSettings.getTag()) {
676     case DemuxIpFilterSettingsFilterSettings::section: {
677         ip.filterSettings.section(getHidlSectionSettings(
678                 ipConf.filterSettings.get<DemuxIpFilterSettingsFilterSettings::section>()));
679         break;
680     }
681     case DemuxIpFilterSettingsFilterSettings::bPassthrough: {
682         ip.filterSettings.bPassthrough(
683                 ipConf.filterSettings.get<DemuxIpFilterSettingsFilterSettings::bPassthrough>());
684         break;
685     }
686     default: {
687         ip.filterSettings.noinit();
688         break;
689     }
690     }
691     hidlSettings.ip(ip);
692 }
693 
getIpV6Address(const DemuxIpAddressIpAddress & addr)694 hidl_array<uint8_t, IP_V6_LENGTH> TunerHidlFilter::getIpV6Address(
695         const DemuxIpAddressIpAddress& addr) {
696     hidl_array<uint8_t, IP_V6_LENGTH> ip;
697     if (addr.get<DemuxIpAddressIpAddress::v6>().size() != IP_V6_LENGTH) {
698         return ip;
699     }
700     copy(addr.get<DemuxIpAddressIpAddress::v6>().begin(),
701          addr.get<DemuxIpAddressIpAddress::v6>().end(), ip.data());
702     return ip;
703 }
704 
getIpV4Address(const DemuxIpAddressIpAddress & addr)705 hidl_array<uint8_t, IP_V4_LENGTH> TunerHidlFilter::getIpV4Address(
706         const DemuxIpAddressIpAddress& addr) {
707     hidl_array<uint8_t, IP_V4_LENGTH> ip;
708     if (addr.get<DemuxIpAddressIpAddress::v4>().size() != IP_V4_LENGTH) {
709         return ip;
710     }
711     copy(addr.get<DemuxIpAddressIpAddress::v4>().begin(),
712          addr.get<DemuxIpAddressIpAddress::v4>().end(), ip.data());
713     return ip;
714 }
715 
getHidlTlvSettings(const DemuxFilterSettings & settings,HidlDemuxFilterSettings & hidlSettings)716 void TunerHidlFilter::getHidlTlvSettings(const DemuxFilterSettings& settings,
717                                          HidlDemuxFilterSettings& hidlSettings) {
718     const DemuxTlvFilterSettings& tlvConf = settings.get<DemuxFilterSettings::tlv>();
719     HidlDemuxTlvFilterSettings tlv{
720             .packetType = static_cast<uint8_t>(tlvConf.packetType),
721             .isCompressedIpPacket = tlvConf.isCompressedIpPacket,
722     };
723 
724     switch (tlvConf.filterSettings.getTag()) {
725     case DemuxTlvFilterSettingsFilterSettings::section: {
726         tlv.filterSettings.section(getHidlSectionSettings(
727                 tlvConf.filterSettings.get<DemuxTlvFilterSettingsFilterSettings::section>()));
728         break;
729     }
730     case DemuxTlvFilterSettingsFilterSettings::bPassthrough: {
731         tlv.filterSettings.bPassthrough(
732                 tlvConf.filterSettings.get<DemuxTlvFilterSettingsFilterSettings::bPassthrough>());
733         break;
734     }
735     default: {
736         tlv.filterSettings.noinit();
737         break;
738     }
739     }
740     hidlSettings.tlv(tlv);
741 }
742 
getHidlAlpSettings(const DemuxFilterSettings & settings,HidlDemuxFilterSettings & hidlSettings)743 void TunerHidlFilter::getHidlAlpSettings(const DemuxFilterSettings& settings,
744                                          HidlDemuxFilterSettings& hidlSettings) {
745     const DemuxAlpFilterSettings& alpConf = settings.get<DemuxFilterSettings::alp>();
746     HidlDemuxAlpFilterSettings alp{
747             .packetType = static_cast<uint8_t>(alpConf.packetType),
748             .lengthType = static_cast<HidlDemuxAlpLengthType>(alpConf.lengthType),
749     };
750 
751     switch (alpConf.filterSettings.getTag()) {
752     case DemuxAlpFilterSettingsFilterSettings::section: {
753         alp.filterSettings.section(getHidlSectionSettings(
754                 alpConf.filterSettings.get<DemuxAlpFilterSettingsFilterSettings::section>()));
755         break;
756     }
757     default: {
758         alp.filterSettings.noinit();
759         break;
760     }
761     }
762     hidlSettings.alp(alp);
763 }
764 
getHidlAvSettings(const DemuxFilterAvSettings & settings)765 HidlDemuxFilterAvSettings TunerHidlFilter::getHidlAvSettings(
766         const DemuxFilterAvSettings& settings) {
767     HidlDemuxFilterAvSettings av{
768             .isPassthrough = settings.isPassthrough,
769     };
770     return av;
771 }
772 
getHidlSectionSettings(const DemuxFilterSectionSettings & settings)773 HidlDemuxFilterSectionSettings TunerHidlFilter::getHidlSectionSettings(
774         const DemuxFilterSectionSettings& settings) {
775     HidlDemuxFilterSectionSettings section{
776             .isCheckCrc = settings.isCheckCrc,
777             .isRepeat = settings.isRepeat,
778             .isRaw = settings.isRaw,
779     };
780 
781     switch (settings.condition.getTag()) {
782     case DemuxFilterSectionSettingsCondition::sectionBits: {
783         const DemuxFilterSectionBits& sectionBits =
784                 settings.condition.get<DemuxFilterSectionSettingsCondition::sectionBits>();
785         vector<uint8_t> filter(sectionBits.filter.begin(), sectionBits.filter.end());
786         vector<uint8_t> mask(sectionBits.mask.begin(), sectionBits.mask.end());
787         vector<uint8_t> mode(sectionBits.mode.begin(), sectionBits.mode.end());
788         section.condition.sectionBits({
789                 .filter = filter,
790                 .mask = mask,
791                 .mode = mode,
792         });
793         break;
794     }
795     case DemuxFilterSectionSettingsCondition::tableInfo: {
796         const DemuxFilterSectionSettingsConditionTableInfo& tableInfo =
797                 settings.condition.get<DemuxFilterSectionSettingsCondition::tableInfo>();
798         section.condition.tableInfo({
799                 .tableId = static_cast<uint16_t>(tableInfo.tableId),
800                 .version = static_cast<uint16_t>(tableInfo.version),
801         });
802         break;
803     }
804     default: {
805         break;
806     }
807     }
808     return section;
809 }
810 
getHidlPesDataSettings(const DemuxFilterPesDataSettings & settings)811 HidlDemuxFilterPesDataSettings TunerHidlFilter::getHidlPesDataSettings(
812         const DemuxFilterPesDataSettings& settings) {
813     HidlDemuxFilterPesDataSettings pes{
814             .streamId = static_cast<HidlDemuxStreamId>(settings.streamId),
815             .isRaw = settings.isRaw,
816     };
817     return pes;
818 }
819 
getHidlRecordSettings(const DemuxFilterRecordSettings & settings)820 HidlDemuxFilterRecordSettings TunerHidlFilter::getHidlRecordSettings(
821         const DemuxFilterRecordSettings& settings) {
822     HidlDemuxFilterRecordSettings record{
823             .tsIndexMask = static_cast<uint32_t>(settings.tsIndexMask),
824     };
825 
826     switch (settings.scIndexMask.getTag()) {
827     case DemuxFilterScIndexMask::scIndex: {
828         record.scIndexType = static_cast<HidlDemuxRecordScIndexType>(settings.scIndexType);
829         record.scIndexMask.sc(
830                 static_cast<uint32_t>(settings.scIndexMask.get<DemuxFilterScIndexMask::scIndex>()));
831         break;
832     }
833     case DemuxFilterScIndexMask::scAvc: {
834         record.scIndexType = HidlDemuxRecordScIndexType::SC;
835         uint32_t index =
836                 static_cast<uint32_t>(settings.scIndexMask.get<DemuxFilterScIndexMask::scAvc>());
837         // HIDL HAL starting from 1 << 4; AIDL starting from 1 << 0.
838         index = index << 4;
839         record.scIndexMask.sc(index);
840         break;
841     }
842     case DemuxFilterScIndexMask::scHevc: {
843         record.scIndexType = static_cast<HidlDemuxRecordScIndexType>(settings.scIndexType);
844         record.scIndexMask.scHevc(
845                 static_cast<uint32_t>(settings.scIndexMask.get<DemuxFilterScIndexMask::scHevc>()));
846         break;
847     }
848     case DemuxFilterScIndexMask::scVvc: {
849         // Shouldn't be here.
850         break;
851     }
852     }
853     return record;
854 }
855 
getHidlDownloadSettings(const DemuxFilterDownloadSettings & settings)856 HidlDemuxFilterDownloadSettings TunerHidlFilter::getHidlDownloadSettings(
857         const DemuxFilterDownloadSettings& settings) {
858     HidlDemuxFilterDownloadSettings download{
859             .downloadId = static_cast<uint32_t>(settings.downloadId),
860     };
861     return download;
862 }
863 
864 /////////////// FilterCallback ///////////////////////
onFilterStatus(HidlDemuxFilterStatus status)865 Return<void> TunerHidlFilter::FilterCallback::onFilterStatus(HidlDemuxFilterStatus status) {
866     Mutex::Autolock _l(mCallbackLock);
867     if (mTunerFilterCallback != nullptr) {
868         mTunerFilterCallback->onFilterStatus(static_cast<DemuxFilterStatus>(status));
869     }
870     return Void();
871 }
872 
onFilterEvent(const HidlDemuxFilterEvent & filterEvent)873 Return<void> TunerHidlFilter::FilterCallback::onFilterEvent(
874         const HidlDemuxFilterEvent& filterEvent) {
875     vector<HidlDemuxFilterEventExt::Event> emptyEventsExt;
876     HidlDemuxFilterEventExt emptyFilterEventExt{
877             .events = emptyEventsExt,
878     };
879     onFilterEvent_1_1(filterEvent, emptyFilterEventExt);
880     return Void();
881 }
882 
onFilterEvent_1_1(const HidlDemuxFilterEvent & filterEvent,const HidlDemuxFilterEventExt & filterEventExt)883 Return<void> TunerHidlFilter::FilterCallback::onFilterEvent_1_1(
884         const HidlDemuxFilterEvent& filterEvent, const HidlDemuxFilterEventExt& filterEventExt) {
885     Mutex::Autolock _l(mCallbackLock);
886     if (mTunerFilterCallback != nullptr) {
887         vector<HidlDemuxFilterEvent::Event> events = filterEvent.events;
888         vector<HidlDemuxFilterEventExt::Event> eventsExt = filterEventExt.events;
889         vector<DemuxFilterEvent> tunerEvents;
890 
891         getAidlFilterEvent(events, eventsExt, tunerEvents);
892         mTunerFilterCallback->onFilterEvent(tunerEvents);
893     }
894     return Void();
895 }
896 
sendSharedFilterStatus(int32_t status)897 void TunerHidlFilter::FilterCallback::sendSharedFilterStatus(int32_t status) {
898     Mutex::Autolock _l(mCallbackLock);
899     if (mTunerFilterCallback != nullptr && mOriginalCallback != nullptr) {
900         mTunerFilterCallback->onFilterStatus(static_cast<DemuxFilterStatus>(status));
901     }
902 }
903 
attachSharedFilterCallback(const shared_ptr<ITunerFilterCallback> & in_cb)904 void TunerHidlFilter::FilterCallback::attachSharedFilterCallback(
905         const shared_ptr<ITunerFilterCallback>& in_cb) {
906     Mutex::Autolock _l(mCallbackLock);
907     mOriginalCallback = mTunerFilterCallback;
908     mTunerFilterCallback = in_cb;
909 }
910 
detachSharedFilterCallback()911 void TunerHidlFilter::FilterCallback::detachSharedFilterCallback() {
912     Mutex::Autolock _l(mCallbackLock);
913     if (mTunerFilterCallback != nullptr && mOriginalCallback != nullptr) {
914         mTunerFilterCallback = mOriginalCallback;
915         mOriginalCallback = nullptr;
916     }
917 }
918 
detachCallbacks()919 void TunerHidlFilter::FilterCallback::detachCallbacks() {
920     Mutex::Autolock _l(mCallbackLock);
921     mOriginalCallback = nullptr;
922     mTunerFilterCallback = nullptr;
923 }
924 
925 /////////////// FilterCallback Helper Methods ///////////////////////
getAidlFilterEvent(const vector<HidlDemuxFilterEvent::Event> & events,const vector<HidlDemuxFilterEventExt::Event> & eventsExt,vector<DemuxFilterEvent> & aidlEvents)926 void TunerHidlFilter::FilterCallback::getAidlFilterEvent(
927         const vector<HidlDemuxFilterEvent::Event>& events,
928         const vector<HidlDemuxFilterEventExt::Event>& eventsExt,
929         vector<DemuxFilterEvent>& aidlEvents) {
930     if (events.empty() && !eventsExt.empty()) {
931         switch (eventsExt[0].getDiscriminator()) {
932         case HidlDemuxFilterEventExt::Event::hidl_discriminator::monitorEvent: {
933             getMonitorEvent(eventsExt, aidlEvents);
934             break;
935         }
936         case HidlDemuxFilterEventExt::Event::hidl_discriminator::startId: {
937             getRestartEvent(eventsExt, aidlEvents);
938             break;
939         }
940         default: {
941             break;
942         }
943         }
944     }
945 
946     if (!events.empty()) {
947         switch (events[0].getDiscriminator()) {
948         case HidlDemuxFilterEvent::Event::hidl_discriminator::media: {
949             getMediaEvent(events, aidlEvents);
950             break;
951         }
952         case HidlDemuxFilterEvent::Event::hidl_discriminator::section: {
953             getSectionEvent(events, aidlEvents);
954             break;
955         }
956         case HidlDemuxFilterEvent::Event::hidl_discriminator::pes: {
957             getPesEvent(events, aidlEvents);
958             break;
959         }
960         case HidlDemuxFilterEvent::Event::hidl_discriminator::tsRecord: {
961             getTsRecordEvent(events, eventsExt, aidlEvents);
962             break;
963         }
964         case HidlDemuxFilterEvent::Event::hidl_discriminator::mmtpRecord: {
965             getMmtpRecordEvent(events, eventsExt, aidlEvents);
966             break;
967         }
968         case HidlDemuxFilterEvent::Event::hidl_discriminator::download: {
969             getDownloadEvent(events, aidlEvents);
970             break;
971         }
972         case HidlDemuxFilterEvent::Event::hidl_discriminator::ipPayload: {
973             getIpPayloadEvent(events, aidlEvents);
974             break;
975         }
976         case HidlDemuxFilterEvent::Event::hidl_discriminator::temi: {
977             getTemiEvent(events, aidlEvents);
978             break;
979         }
980         default: {
981             break;
982         }
983         }
984     }
985 }
986 
getMediaEvent(const vector<HidlDemuxFilterEvent::Event> & events,vector<DemuxFilterEvent> & res)987 void TunerHidlFilter::FilterCallback::getMediaEvent(
988         const vector<HidlDemuxFilterEvent::Event>& events, vector<DemuxFilterEvent>& res) {
989     for (int i = 0; i < events.size(); i++) {
990         const HidlDemuxFilterMediaEvent& mediaEvent = events[i].media();
991         DemuxFilterMediaEvent media;
992 
993         media.streamId = static_cast<int32_t>(mediaEvent.streamId);
994         media.isPtsPresent = mediaEvent.isPtsPresent;
995         media.pts = static_cast<int64_t>(mediaEvent.pts);
996         media.isDtsPresent = false;
997         media.dts = static_cast<int64_t>(-1);
998         media.dataLength = static_cast<int64_t>(mediaEvent.dataLength);
999         media.offset = static_cast<int64_t>(mediaEvent.offset);
1000         media.isSecureMemory = mediaEvent.isSecureMemory;
1001         media.avDataId = static_cast<int64_t>(mediaEvent.avDataId);
1002         media.mpuSequenceNumber = static_cast<int32_t>(mediaEvent.mpuSequenceNumber);
1003         media.isPesPrivateData = mediaEvent.isPesPrivateData;
1004         media.scIndexMask.set<DemuxFilterScIndexMask::scIndex>(
1005                 static_cast<int32_t>(DemuxScIndex::UNDEFINED));
1006 
1007         if (mediaEvent.extraMetaData.getDiscriminator() ==
1008             HidlDemuxFilterMediaEvent::ExtraMetaData::hidl_discriminator::audio) {
1009             AudioExtraMetaData audio;
1010             audio.adFade = static_cast<int8_t>(mediaEvent.extraMetaData.audio().adFade);
1011             audio.adPan = static_cast<int8_t>(mediaEvent.extraMetaData.audio().adPan);
1012             audio.versionTextTag =
1013                     static_cast<int16_t>(mediaEvent.extraMetaData.audio().versionTextTag);
1014             audio.adGainCenter = static_cast<int8_t>(mediaEvent.extraMetaData.audio().adGainCenter);
1015             audio.adGainFront = static_cast<int8_t>(mediaEvent.extraMetaData.audio().adGainFront);
1016             audio.adGainSurround =
1017                     static_cast<int8_t>(mediaEvent.extraMetaData.audio().adGainSurround);
1018             media.extraMetaData.set<DemuxFilterMediaEventExtraMetaData::audio>(audio);
1019         } else {
1020             media.extraMetaData.set<DemuxFilterMediaEventExtraMetaData::noinit>(true);
1021         }
1022 
1023         if (mediaEvent.avMemory.getNativeHandle() != nullptr) {
1024             media.avMemory = dupToAidl(mediaEvent.avMemory.getNativeHandle());
1025         }
1026 
1027         DemuxFilterEvent filterEvent;
1028         filterEvent.set<DemuxFilterEvent::media>(std::move(media));
1029         res.push_back(std::move(filterEvent));
1030     }
1031 }
1032 
getSectionEvent(const vector<HidlDemuxFilterEvent::Event> & events,vector<DemuxFilterEvent> & res)1033 void TunerHidlFilter::FilterCallback::getSectionEvent(
1034         const vector<HidlDemuxFilterEvent::Event>& events, vector<DemuxFilterEvent>& res) {
1035     for (int i = 0; i < events.size(); i++) {
1036         const HidlDemuxFilterSectionEvent& sectionEvent = events[i].section();
1037         DemuxFilterSectionEvent section;
1038 
1039         section.tableId = static_cast<int32_t>(sectionEvent.tableId);
1040         section.version = static_cast<int32_t>(sectionEvent.version);
1041         section.sectionNum = static_cast<int32_t>(sectionEvent.sectionNum);
1042         section.dataLength = static_cast<int64_t>(sectionEvent.dataLength);
1043 
1044         DemuxFilterEvent filterEvent;
1045         filterEvent.set<DemuxFilterEvent::section>(std::move(section));
1046         res.push_back(std::move(filterEvent));
1047     }
1048 }
1049 
getPesEvent(const vector<HidlDemuxFilterEvent::Event> & events,vector<DemuxFilterEvent> & res)1050 void TunerHidlFilter::FilterCallback::getPesEvent(const vector<HidlDemuxFilterEvent::Event>& events,
1051                                                   vector<DemuxFilterEvent>& res) {
1052     for (int i = 0; i < events.size(); i++) {
1053         const HidlDemuxFilterPesEvent& pesEvent = events[i].pes();
1054         DemuxFilterPesEvent pes;
1055 
1056         pes.streamId = static_cast<int32_t>(pesEvent.streamId);
1057         pes.dataLength = static_cast<int32_t>(pesEvent.dataLength);
1058         pes.mpuSequenceNumber = static_cast<int32_t>(pesEvent.mpuSequenceNumber);
1059 
1060         DemuxFilterEvent filterEvent;
1061         filterEvent.set<DemuxFilterEvent::pes>(std::move(pes));
1062         res.push_back(std::move(filterEvent));
1063     }
1064 }
1065 
getTsRecordEvent(const vector<HidlDemuxFilterEvent::Event> & events,const vector<HidlDemuxFilterEventExt::Event> & eventsExt,vector<DemuxFilterEvent> & res)1066 void TunerHidlFilter::FilterCallback::getTsRecordEvent(
1067         const vector<HidlDemuxFilterEvent::Event>& events,
1068         const vector<HidlDemuxFilterEventExt::Event>& eventsExt, vector<DemuxFilterEvent>& res) {
1069     for (int i = 0; i < events.size(); i++) {
1070         DemuxFilterTsRecordEvent tsRecord;
1071         const HidlDemuxFilterTsRecordEvent& tsRecordEvent = events[i].tsRecord();
1072 
1073         DemuxFilterScIndexMask scIndexMask;
1074         if (tsRecordEvent.scIndexMask.getDiscriminator() ==
1075             HidlDemuxFilterTsRecordEvent::ScIndexMask::hidl_discriminator::sc) {
1076             int32_t hidlScIndex = static_cast<int32_t>(tsRecordEvent.scIndexMask.sc());
1077             if (hidlScIndex <= static_cast<int32_t>(DemuxScIndex::SEQUENCE)) {
1078                 scIndexMask.set<DemuxFilterScIndexMask::scIndex>(hidlScIndex);
1079             } else {
1080                 // HIDL HAL starting from 1 << 4; AIDL starting from 1 << 0.
1081                 scIndexMask.set<DemuxFilterScIndexMask::scAvc>(hidlScIndex >> 4);
1082             }
1083         } else if (tsRecordEvent.scIndexMask.getDiscriminator() ==
1084                    HidlDemuxFilterTsRecordEvent::ScIndexMask::hidl_discriminator::scHevc) {
1085             scIndexMask.set<DemuxFilterScIndexMask::scHevc>(
1086                     static_cast<int32_t>(tsRecordEvent.scIndexMask.scHevc()));
1087         }
1088 
1089         if (tsRecordEvent.pid.getDiscriminator() == HidlDemuxPid::hidl_discriminator::tPid) {
1090             DemuxPid pid;
1091             pid.set<DemuxPid::tPid>(static_cast<int32_t>(tsRecordEvent.pid.tPid()));
1092             tsRecord.pid = pid;
1093         } else {
1094             DemuxPid pid;
1095             pid.set<DemuxPid::tPid>(static_cast<int32_t>(Constant::INVALID_TS_PID));
1096             tsRecord.pid = pid;
1097         }
1098 
1099         tsRecord.scIndexMask = scIndexMask;
1100         tsRecord.tsIndexMask = static_cast<int32_t>(tsRecordEvent.tsIndexMask);
1101         tsRecord.byteNumber = static_cast<int64_t>(tsRecordEvent.byteNumber);
1102 
1103         if (eventsExt.size() > i &&
1104             eventsExt[i].getDiscriminator() ==
1105                     HidlDemuxFilterEventExt::Event::hidl_discriminator::tsRecord) {
1106             tsRecord.pts = static_cast<int64_t>(eventsExt[i].tsRecord().pts);
1107             tsRecord.firstMbInSlice = static_cast<int32_t>(eventsExt[i].tsRecord().firstMbInSlice);
1108         }
1109 
1110         DemuxFilterEvent filterEvent;
1111         filterEvent.set<DemuxFilterEvent::tsRecord>(std::move(tsRecord));
1112         res.push_back(std::move(filterEvent));
1113     }
1114 }
1115 
getMmtpRecordEvent(const vector<HidlDemuxFilterEvent::Event> & events,const vector<HidlDemuxFilterEventExt::Event> & eventsExt,vector<DemuxFilterEvent> & res)1116 void TunerHidlFilter::FilterCallback::getMmtpRecordEvent(
1117         const vector<HidlDemuxFilterEvent::Event>& events,
1118         const vector<HidlDemuxFilterEventExt::Event>& eventsExt, vector<DemuxFilterEvent>& res) {
1119     for (int i = 0; i < events.size(); i++) {
1120         DemuxFilterMmtpRecordEvent mmtpRecord;
1121         const HidlDemuxFilterMmtpRecordEvent& mmtpRecordEvent = events[i].mmtpRecord();
1122 
1123         mmtpRecord.scHevcIndexMask = static_cast<int32_t>(mmtpRecordEvent.scHevcIndexMask);
1124         mmtpRecord.byteNumber = static_cast<int64_t>(mmtpRecordEvent.byteNumber);
1125 
1126         if (eventsExt.size() > i &&
1127             eventsExt[i].getDiscriminator() ==
1128                     HidlDemuxFilterEventExt::Event::hidl_discriminator::mmtpRecord) {
1129             mmtpRecord.pts = static_cast<int64_t>(eventsExt[i].mmtpRecord().pts);
1130             mmtpRecord.mpuSequenceNumber =
1131                     static_cast<int32_t>(eventsExt[i].mmtpRecord().mpuSequenceNumber);
1132             mmtpRecord.firstMbInSlice =
1133                     static_cast<int32_t>(eventsExt[i].mmtpRecord().firstMbInSlice);
1134             mmtpRecord.tsIndexMask = static_cast<int32_t>(eventsExt[i].mmtpRecord().tsIndexMask);
1135         }
1136 
1137         DemuxFilterEvent filterEvent;
1138         filterEvent.set<DemuxFilterEvent::mmtpRecord>(std::move(mmtpRecord));
1139         res.push_back(std::move(filterEvent));
1140     }
1141 }
1142 
getDownloadEvent(const vector<HidlDemuxFilterEvent::Event> & events,vector<DemuxFilterEvent> & res)1143 void TunerHidlFilter::FilterCallback::getDownloadEvent(
1144         const vector<HidlDemuxFilterEvent::Event>& events, vector<DemuxFilterEvent>& res) {
1145     for (int i = 0; i < events.size(); i++) {
1146         const HidlDemuxFilterDownloadEvent& downloadEvent = events[i].download();
1147         DemuxFilterDownloadEvent download;
1148 
1149         download.itemId = static_cast<int32_t>(downloadEvent.itemId);
1150         download.downloadId = -1;
1151         download.itemFragmentIndex = static_cast<int32_t>(downloadEvent.itemFragmentIndex);
1152         download.mpuSequenceNumber = static_cast<int32_t>(downloadEvent.mpuSequenceNumber);
1153         download.lastItemFragmentIndex = static_cast<int32_t>(downloadEvent.lastItemFragmentIndex);
1154         download.dataLength = static_cast<int32_t>(downloadEvent.dataLength);
1155 
1156         DemuxFilterEvent filterEvent;
1157         filterEvent.set<DemuxFilterEvent::download>(std::move(download));
1158         res.push_back(std::move(filterEvent));
1159     }
1160 }
1161 
getIpPayloadEvent(const vector<HidlDemuxFilterEvent::Event> & events,vector<DemuxFilterEvent> & res)1162 void TunerHidlFilter::FilterCallback::getIpPayloadEvent(
1163         const vector<HidlDemuxFilterEvent::Event>& events, vector<DemuxFilterEvent>& res) {
1164     for (int i = 0; i < events.size(); i++) {
1165         const HidlDemuxFilterIpPayloadEvent& ipPayloadEvent = events[i].ipPayload();
1166         DemuxFilterIpPayloadEvent ipPayload;
1167 
1168         ipPayload.dataLength = static_cast<int32_t>(ipPayloadEvent.dataLength);
1169 
1170         DemuxFilterEvent filterEvent;
1171         filterEvent.set<DemuxFilterEvent::ipPayload>(std::move(ipPayload));
1172         res.push_back(std::move(filterEvent));
1173     }
1174 }
1175 
getTemiEvent(const vector<HidlDemuxFilterEvent::Event> & events,vector<DemuxFilterEvent> & res)1176 void TunerHidlFilter::FilterCallback::getTemiEvent(
1177         const vector<HidlDemuxFilterEvent::Event>& events, vector<DemuxFilterEvent>& res) {
1178     for (int i = 0; i < events.size(); i++) {
1179         const HidlDemuxFilterTemiEvent& temiEvent = events[i].temi();
1180         DemuxFilterTemiEvent temi;
1181 
1182         temi.pts = static_cast<int64_t>(temiEvent.pts);
1183         temi.descrTag = static_cast<int8_t>(temiEvent.descrTag);
1184         vector<uint8_t> descrData = temiEvent.descrData;
1185         temi.descrData.resize(descrData.size());
1186         copy(descrData.begin(), descrData.end(), temi.descrData.begin());
1187 
1188         DemuxFilterEvent filterEvent;
1189         filterEvent.set<DemuxFilterEvent::temi>(std::move(temi));
1190         res.push_back(std::move(filterEvent));
1191     }
1192 }
1193 
getMonitorEvent(const vector<HidlDemuxFilterEventExt::Event> & eventsExt,vector<DemuxFilterEvent> & res)1194 void TunerHidlFilter::FilterCallback::getMonitorEvent(
1195         const vector<HidlDemuxFilterEventExt::Event>& eventsExt, vector<DemuxFilterEvent>& res) {
1196     HidlDemuxFilterMonitorEvent monitorEvent = eventsExt[0].monitorEvent();
1197     DemuxFilterMonitorEvent monitor;
1198 
1199     switch (monitorEvent.getDiscriminator()) {
1200     case HidlDemuxFilterMonitorEvent::hidl_discriminator::scramblingStatus: {
1201         monitor.set<DemuxFilterMonitorEvent::scramblingStatus>(
1202                 static_cast<ScramblingStatus>(monitorEvent.scramblingStatus()));
1203         break;
1204     }
1205     case HidlDemuxFilterMonitorEvent::hidl_discriminator::cid: {
1206         monitor.set<DemuxFilterMonitorEvent::cid>(static_cast<int32_t>(monitorEvent.cid()));
1207         break;
1208     }
1209     }
1210 
1211     DemuxFilterEvent filterEvent;
1212     filterEvent.set<DemuxFilterEvent::monitorEvent>(std::move(monitor));
1213     res.push_back(std::move(filterEvent));
1214 }
1215 
getRestartEvent(const vector<HidlDemuxFilterEventExt::Event> & eventsExt,vector<DemuxFilterEvent> & res)1216 void TunerHidlFilter::FilterCallback::getRestartEvent(
1217         const vector<HidlDemuxFilterEventExt::Event>& eventsExt, vector<DemuxFilterEvent>& res) {
1218     DemuxFilterEvent filterEvent;
1219     filterEvent.set<DemuxFilterEvent::startId>(static_cast<int32_t>(eventsExt[0].startId()));
1220     res.push_back(std::move(filterEvent));
1221 }
1222 
1223 }  // namespace tuner
1224 }  // namespace tv
1225 }  // namespace media
1226 }  // namespace android
1227 }  // namespace aidl
1228