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_TUNERHIDLFILTER_H 18 #define ANDROID_MEDIA_TUNERHIDLFILTER_H 19 20 #include <aidl/android/hardware/tv/tuner/AvStreamType.h> 21 #include <aidl/android/hardware/tv/tuner/DemuxFilterAvSettings.h> 22 #include <aidl/android/hardware/tv/tuner/DemuxFilterDownloadSettings.h> 23 #include <aidl/android/hardware/tv/tuner/DemuxFilterEvent.h> 24 #include <aidl/android/hardware/tv/tuner/DemuxFilterPesDataSettings.h> 25 #include <aidl/android/hardware/tv/tuner/DemuxFilterRecordSettings.h> 26 #include <aidl/android/hardware/tv/tuner/DemuxFilterSectionSettings.h> 27 #include <aidl/android/hardware/tv/tuner/DemuxFilterSettings.h> 28 #include <aidl/android/hardware/tv/tuner/DemuxFilterStatus.h> 29 #include <aidl/android/hardware/tv/tuner/DemuxFilterType.h> 30 #include <aidl/android/media/tv/tuner/BnTunerFilter.h> 31 #include <aidl/android/media/tv/tuner/ITunerFilterCallback.h> 32 #include <android/hardware/tv/tuner/1.0/ITuner.h> 33 #include <android/hardware/tv/tuner/1.1/IFilter.h> 34 #include <android/hardware/tv/tuner/1.1/IFilterCallback.h> 35 #include <android/hardware/tv/tuner/1.1/types.h> 36 #include <fmq/MessageQueue.h> 37 #include <utils/Mutex.h> 38 39 #include <map> 40 41 using ::aidl::android::hardware::common::NativeHandle; 42 using ::aidl::android::hardware::common::fmq::MQDescriptor; 43 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; 44 using ::aidl::android::hardware::tv::tuner::AvStreamType; 45 using ::aidl::android::hardware::tv::tuner::DemuxFilterAvSettings; 46 using ::aidl::android::hardware::tv::tuner::DemuxFilterDownloadSettings; 47 using ::aidl::android::hardware::tv::tuner::DemuxFilterEvent; 48 using ::aidl::android::hardware::tv::tuner::DemuxFilterPesDataSettings; 49 using ::aidl::android::hardware::tv::tuner::DemuxFilterRecordSettings; 50 using ::aidl::android::hardware::tv::tuner::DemuxFilterSectionSettings; 51 using ::aidl::android::hardware::tv::tuner::DemuxFilterSettings; 52 using ::aidl::android::hardware::tv::tuner::DemuxFilterStatus; 53 using ::aidl::android::hardware::tv::tuner::DemuxFilterType; 54 using ::aidl::android::hardware::tv::tuner::DemuxIpAddressIpAddress; 55 using ::aidl::android::hardware::tv::tuner::FilterDelayHint; 56 using ::aidl::android::media::tv::tuner::BnTunerFilter; 57 using ::aidl::android::media::tv::tuner::ITunerFilterCallback; 58 using ::android::Mutex; 59 using ::android::sp; 60 using ::android::hardware::hidl_array; 61 using ::android::hardware::MQDescriptorSync; 62 using ::android::hardware::Return; 63 using ::android::hardware::Void; 64 using ::std::shared_ptr; 65 using ::std::string; 66 using ::std::vector; 67 68 using HidlAvStreamType = ::android::hardware::tv::tuner::V1_1::AvStreamType; 69 using HidlDemuxAlpFilterSettings = ::android::hardware::tv::tuner::V1_0::DemuxAlpFilterSettings; 70 using HidlDemuxFilterAvSettings = ::android::hardware::tv::tuner::V1_0::DemuxFilterAvSettings; 71 using HidlDemuxFilterDownloadEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterDownloadEvent; 72 using HidlDemuxFilterDownloadSettings = 73 ::android::hardware::tv::tuner::V1_0::DemuxFilterDownloadSettings; 74 using HidlDemuxFilterIpPayloadEvent = 75 ::android::hardware::tv::tuner::V1_0::DemuxFilterIpPayloadEvent; 76 using HidlDemuxFilterEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterEvent; 77 using HidlDemuxFilterMediaEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterMediaEvent; 78 using HidlDemuxFilterMmtpRecordEvent = 79 ::android::hardware::tv::tuner::V1_0::DemuxFilterMmtpRecordEvent; 80 using HidlDemuxFilterPesDataSettings = 81 ::android::hardware::tv::tuner::V1_0::DemuxFilterPesDataSettings; 82 using HidlDemuxFilterPesEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterPesEvent; 83 using HidlDemuxFilterRecordSettings = 84 ::android::hardware::tv::tuner::V1_0::DemuxFilterRecordSettings; 85 using HidlDemuxFilterSectionEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterSectionEvent; 86 using HidlDemuxFilterSectionSettings = 87 ::android::hardware::tv::tuner::V1_0::DemuxFilterSectionSettings; 88 using HidlDemuxFilterSettings = ::android::hardware::tv::tuner::V1_0::DemuxFilterSettings; 89 using HidlDemuxFilterStatus = ::android::hardware::tv::tuner::V1_0::DemuxFilterStatus; 90 using HidlDemuxFilterTemiEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterTemiEvent; 91 using HidlDemuxFilterTsRecordEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterTsRecordEvent; 92 using HidlDemuxIpFilterSettings = ::android::hardware::tv::tuner::V1_0::DemuxIpFilterSettings; 93 using HidlDemuxMmtpFilterSettings = ::android::hardware::tv::tuner::V1_0::DemuxMmtpFilterSettings; 94 using HidlDemuxTlvFilterSettings = ::android::hardware::tv::tuner::V1_0::DemuxTlvFilterSettings; 95 using HidlDemuxTsFilterSettings = ::android::hardware::tv::tuner::V1_0::DemuxTsFilterSettings; 96 using HidlDemuxPid = ::android::hardware::tv::tuner::V1_0::DemuxPid; 97 using HidlIFilter = ::android::hardware::tv::tuner::V1_0::IFilter; 98 using HidlDvStreamType = ::android::hardware::tv::tuner::V1_1::AvStreamType; 99 using HidlDemuxFilterEventExt = ::android::hardware::tv::tuner::V1_1::DemuxFilterEventExt; 100 using HidlDemuxFilterMonitorEvent = ::android::hardware::tv::tuner::V1_1::DemuxFilterMonitorEvent; 101 using HidlDemuxFilterTsRecordEventExt = 102 ::android::hardware::tv::tuner::V1_1::DemuxFilterTsRecordEventExt; 103 using HidlIFilterCallback = ::android::hardware::tv::tuner::V1_1::IFilterCallback; 104 105 namespace aidl { 106 namespace android { 107 namespace media { 108 namespace tv { 109 namespace tuner { 110 111 using MQDesc = MQDescriptorSync<uint8_t>; 112 using AidlMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>; 113 114 const static int IP_V4_LENGTH = 4; 115 const static int IP_V6_LENGTH = 16; 116 117 class TunerHidlService; 118 119 class TunerHidlFilter : public BnTunerFilter { 120 public: 121 class FilterCallback : public HidlIFilterCallback { 122 public: FilterCallback(const shared_ptr<ITunerFilterCallback> tunerFilterCallback)123 FilterCallback(const shared_ptr<ITunerFilterCallback> tunerFilterCallback) 124 : mTunerFilterCallback(tunerFilterCallback){}; 125 126 virtual Return<void> onFilterEvent(const HidlDemuxFilterEvent& filterEvent); 127 virtual Return<void> onFilterEvent_1_1(const HidlDemuxFilterEvent& filterEvent, 128 const HidlDemuxFilterEventExt& filterEventExt); 129 virtual Return<void> onFilterStatus(HidlDemuxFilterStatus status); 130 131 void sendSharedFilterStatus(int32_t status); 132 void attachSharedFilterCallback(const shared_ptr<ITunerFilterCallback>& in_cb); 133 void detachSharedFilterCallback(); 134 void detachCallbacks(); 135 136 private: 137 void getAidlFilterEvent(const vector<HidlDemuxFilterEvent::Event>& events, 138 const vector<HidlDemuxFilterEventExt::Event>& eventsExt, 139 vector<DemuxFilterEvent>& aidlEvents); 140 141 void getMediaEvent(const vector<HidlDemuxFilterEvent::Event>& events, 142 vector<DemuxFilterEvent>& res); 143 void getSectionEvent(const vector<HidlDemuxFilterEvent::Event>& events, 144 vector<DemuxFilterEvent>& res); 145 void getPesEvent(const vector<HidlDemuxFilterEvent::Event>& events, 146 vector<DemuxFilterEvent>& res); 147 void getTsRecordEvent(const vector<HidlDemuxFilterEvent::Event>& events, 148 const vector<HidlDemuxFilterEventExt::Event>& eventsExt, 149 vector<DemuxFilterEvent>& res); 150 void getMmtpRecordEvent(const vector<HidlDemuxFilterEvent::Event>& events, 151 const vector<HidlDemuxFilterEventExt::Event>& eventsExt, 152 vector<DemuxFilterEvent>& res); 153 void getDownloadEvent(const vector<HidlDemuxFilterEvent::Event>& events, 154 vector<DemuxFilterEvent>& res); 155 void getIpPayloadEvent(const vector<HidlDemuxFilterEvent::Event>& events, 156 vector<DemuxFilterEvent>& res); 157 void getTemiEvent(const vector<HidlDemuxFilterEvent::Event>& events, 158 vector<DemuxFilterEvent>& res); 159 void getMonitorEvent(const vector<HidlDemuxFilterEventExt::Event>& eventsExt, 160 vector<DemuxFilterEvent>& res); 161 void getRestartEvent(const vector<HidlDemuxFilterEventExt::Event>& eventsExt, 162 vector<DemuxFilterEvent>& res); 163 164 private: 165 shared_ptr<ITunerFilterCallback> mTunerFilterCallback; 166 shared_ptr<ITunerFilterCallback> mOriginalCallback; 167 Mutex mCallbackLock; 168 }; 169 170 TunerHidlFilter(const sp<HidlIFilter> filter, const sp<FilterCallback> cb, 171 const DemuxFilterType type, const shared_ptr<TunerHidlService> tuner); 172 virtual ~TunerHidlFilter(); 173 174 ::ndk::ScopedAStatus getId(int32_t* _aidl_return) override; 175 ::ndk::ScopedAStatus getId64Bit(int64_t* _aidl_return) override; 176 ::ndk::ScopedAStatus getQueueDesc(AidlMQDesc* _aidl_return) override; 177 ::ndk::ScopedAStatus configure(const DemuxFilterSettings& in_settings) override; 178 ::ndk::ScopedAStatus configureMonitorEvent(int32_t in_monitorEventTypes) override; 179 ::ndk::ScopedAStatus configureIpFilterContextId(int32_t in_cid) override; 180 ::ndk::ScopedAStatus configureAvStreamType(const AvStreamType& in_avStreamType) override; 181 ::ndk::ScopedAStatus getAvSharedHandle(NativeHandle* out_avMemory, 182 int64_t* _aidl_return) override; 183 ::ndk::ScopedAStatus releaseAvHandle(const NativeHandle& in_handle, 184 int64_t in_avDataId) override; 185 ::ndk::ScopedAStatus setDataSource(const shared_ptr<ITunerFilter>& in_filter) override; 186 ::ndk::ScopedAStatus start() override; 187 ::ndk::ScopedAStatus stop() override; 188 ::ndk::ScopedAStatus flush() override; 189 ::ndk::ScopedAStatus close() override; 190 ::ndk::ScopedAStatus acquireSharedFilterToken(string* _aidl_return) override; 191 ::ndk::ScopedAStatus freeSharedFilterToken(const string& in_filterToken) override; 192 ::ndk::ScopedAStatus getFilterType(DemuxFilterType* _aidl_return) override; 193 ::ndk::ScopedAStatus setDelayHint(const FilterDelayHint& in_hint) override; 194 195 bool isSharedFilterAllowed(int32_t pid); 196 void attachSharedFilterCallback(const shared_ptr<ITunerFilterCallback>& in_cb); 197 sp<HidlIFilter> getHalFilter(); 198 199 private: 200 bool isAudioFilter(); 201 bool isVideoFilter(); 202 203 HidlDemuxFilterAvSettings getHidlAvSettings(const DemuxFilterAvSettings& settings); 204 HidlDemuxFilterSectionSettings getHidlSectionSettings( 205 const DemuxFilterSectionSettings& settings); 206 HidlDemuxFilterPesDataSettings getHidlPesDataSettings( 207 const DemuxFilterPesDataSettings& settings); 208 HidlDemuxFilterRecordSettings getHidlRecordSettings(const DemuxFilterRecordSettings& settings); 209 HidlDemuxFilterDownloadSettings getHidlDownloadSettings( 210 const DemuxFilterDownloadSettings& settings); 211 bool getHidlAvStreamType(const AvStreamType avStreamType, HidlAvStreamType& type); 212 void getHidlTsSettings(const DemuxFilterSettings& settings, 213 HidlDemuxFilterSettings& hidlSettings); 214 void getHidlMmtpSettings(const DemuxFilterSettings& settings, 215 HidlDemuxFilterSettings& hidlSettings); 216 void getHidlIpSettings(const DemuxFilterSettings& settings, 217 HidlDemuxFilterSettings& hidlSettings); 218 void getHidlTlvSettings(const DemuxFilterSettings& settings, 219 HidlDemuxFilterSettings& hidlSettings); 220 void getHidlAlpSettings(const DemuxFilterSettings& settings, 221 HidlDemuxFilterSettings& hidlSettings); 222 223 hidl_array<uint8_t, IP_V4_LENGTH> getIpV4Address(const DemuxIpAddressIpAddress& addr); 224 hidl_array<uint8_t, IP_V6_LENGTH> getIpV6Address(const DemuxIpAddressIpAddress& addr); 225 226 sp<HidlIFilter> mFilter; 227 sp<::android::hardware::tv::tuner::V1_1::IFilter> mFilter_1_1; 228 int32_t mId; 229 int64_t mId64Bit; 230 DemuxFilterType mType; 231 bool mStarted; 232 bool mShared; 233 int32_t mClientPid; 234 sp<FilterCallback> mFilterCallback; 235 Mutex mLock; 236 shared_ptr<TunerHidlService> mTunerService; 237 }; 238 239 } // namespace tuner 240 } // namespace tv 241 } // namespace media 242 } // namespace android 243 } // namespace aidl 244 245 #endif // ANDROID_MEDIA_TUNERHIDLFILTER_H 246