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 #ifndef ANDROID_MEDIA_TUNERFILTER_H
18 #define ANDROID_MEDIA_TUNERFILTER_H
19 
20 #include <aidl/android/hardware/tv/tuner/AvStreamType.h>
21 #include <aidl/android/hardware/tv/tuner/BnFilterCallback.h>
22 #include <aidl/android/hardware/tv/tuner/DemuxFilterEvent.h>
23 #include <aidl/android/hardware/tv/tuner/DemuxFilterSettings.h>
24 #include <aidl/android/hardware/tv/tuner/DemuxFilterStatus.h>
25 #include <aidl/android/hardware/tv/tuner/DemuxFilterType.h>
26 #include <aidl/android/hardware/tv/tuner/FilterDelayHint.h>
27 #include <aidl/android/hardware/tv/tuner/IFilter.h>
28 #include <aidl/android/media/tv/tuner/BnTunerFilter.h>
29 #include <aidl/android/media/tv/tuner/ITunerFilterCallback.h>
30 #include <utils/Mutex.h>
31 
32 using ::aidl::android::hardware::common::NativeHandle;
33 using ::aidl::android::hardware::common::fmq::MQDescriptor;
34 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
35 using ::aidl::android::hardware::tv::tuner::AvStreamType;
36 using ::aidl::android::hardware::tv::tuner::BnFilterCallback;
37 using ::aidl::android::hardware::tv::tuner::DemuxFilterEvent;
38 using ::aidl::android::hardware::tv::tuner::DemuxFilterSettings;
39 using ::aidl::android::hardware::tv::tuner::DemuxFilterStatus;
40 using ::aidl::android::hardware::tv::tuner::DemuxFilterType;
41 using ::aidl::android::hardware::tv::tuner::FilterDelayHint;
42 using ::aidl::android::hardware::tv::tuner::IFilter;
43 using ::aidl::android::media::tv::tuner::BnTunerFilter;
44 using ::android::Mutex;
45 
46 using namespace std;
47 
48 namespace aidl {
49 namespace android {
50 namespace media {
51 namespace tv {
52 namespace tuner {
53 
54 using AidlMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>;
55 
56 class TunerService;
57 
58 class TunerFilter : public BnTunerFilter {
59 public:
60     class FilterCallback : public BnFilterCallback {
61     public:
FilterCallback(const shared_ptr<ITunerFilterCallback> & tunerFilterCallback)62         FilterCallback(const shared_ptr<ITunerFilterCallback>& tunerFilterCallback)
63               : mTunerFilterCallback(tunerFilterCallback), mOriginalCallback(nullptr){};
64 
65         ::ndk::ScopedAStatus onFilterEvent(const vector<DemuxFilterEvent>& events) override;
66         ::ndk::ScopedAStatus onFilterStatus(DemuxFilterStatus status) override;
67 
68         void sendSharedFilterStatus(int32_t status);
69         void attachSharedFilterCallback(const shared_ptr<ITunerFilterCallback>& in_cb);
70         void detachSharedFilterCallback();
71         void detachCallbacks();
72 
73     private:
74         shared_ptr<ITunerFilterCallback> mTunerFilterCallback;
75         shared_ptr<ITunerFilterCallback> mOriginalCallback;
76         Mutex mCallbackLock;
77     };
78 
79     TunerFilter(const shared_ptr<IFilter> filter, const shared_ptr<FilterCallback> cb,
80                 const DemuxFilterType type, const shared_ptr<TunerService> tuner);
81     virtual ~TunerFilter();
82 
83     ::ndk::ScopedAStatus getId(int32_t* _aidl_return) override;
84     ::ndk::ScopedAStatus getId64Bit(int64_t* _aidl_return) override;
85     ::ndk::ScopedAStatus getQueueDesc(AidlMQDesc* _aidl_return) override;
86     ::ndk::ScopedAStatus configure(const DemuxFilterSettings& in_settings) override;
87     ::ndk::ScopedAStatus configureMonitorEvent(int32_t in_monitorEventTypes) override;
88     ::ndk::ScopedAStatus configureIpFilterContextId(int32_t in_cid) override;
89     ::ndk::ScopedAStatus configureAvStreamType(const AvStreamType& in_avStreamType) override;
90     ::ndk::ScopedAStatus getAvSharedHandle(NativeHandle* out_avMemory,
91                                            int64_t* _aidl_return) override;
92     ::ndk::ScopedAStatus releaseAvHandle(const NativeHandle& in_handle,
93                                          int64_t in_avDataId) override;
94     ::ndk::ScopedAStatus setDataSource(const shared_ptr<ITunerFilter>& in_filter) override;
95     ::ndk::ScopedAStatus start() override;
96     ::ndk::ScopedAStatus stop() override;
97     ::ndk::ScopedAStatus flush() override;
98     ::ndk::ScopedAStatus close() override;
99     ::ndk::ScopedAStatus acquireSharedFilterToken(string* _aidl_return) override;
100     ::ndk::ScopedAStatus freeSharedFilterToken(const string& in_filterToken) override;
101     ::ndk::ScopedAStatus getFilterType(DemuxFilterType* _aidl_return) override;
102     ::ndk::ScopedAStatus setDelayHint(const FilterDelayHint& in_hint) override;
103 
104     bool isSharedFilterAllowed(int32_t pid);
105     void attachSharedFilterCallback(const shared_ptr<ITunerFilterCallback>& in_cb);
106     shared_ptr<IFilter> getHalFilter();
107 
108 private:
109     shared_ptr<IFilter> mFilter;
110     int32_t mId;
111     int64_t mId64Bit;
112     DemuxFilterType mType;
113     bool mStarted;
114     bool mShared;
115     int32_t mClientPid;
116     shared_ptr<FilterCallback> mFilterCallback;
117     Mutex mLock;
118     shared_ptr<TunerService> mTunerService;
119     bool isClosed = false;
120 };
121 
122 }  // namespace tuner
123 }  // namespace tv
124 }  // namespace media
125 }  // namespace android
126 }  // namespace aidl
127 
128 #endif // ANDROID_MEDIA_TUNERFILTER_H
129