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_TV_FILTER_CLIENT_H_ 18 #define _ANDROID_MEDIA_TV_FILTER_CLIENT_H_ 19 20 #include <aidl/android/hardware/tv/tuner/DemuxFilterType.h> 21 #include <aidl/android/media/tv/tuner/BnTunerFilterCallback.h> 22 #include <aidl/android/media/tv/tuner/ITunerFilter.h> 23 #include <fmq/AidlMessageQueue.h> 24 #include <utils/Mutex.h> 25 26 #include "ClientHelper.h" 27 #include "FilterClientCallback.h" 28 29 using Status = ::ndk::ScopedAStatus; 30 using ::aidl::android::hardware::common::fmq::MQDescriptor; 31 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; 32 using ::aidl::android::hardware::tv::tuner::AvStreamType; 33 using ::aidl::android::hardware::tv::tuner::DemuxFilterEvent; 34 using ::aidl::android::hardware::tv::tuner::DemuxFilterSettings; 35 using ::aidl::android::hardware::tv::tuner::DemuxFilterStatus; 36 using ::aidl::android::hardware::tv::tuner::DemuxFilterType; 37 using ::aidl::android::hardware::tv::tuner::FilterDelayHint; 38 using ::aidl::android::media::tv::tuner::BnTunerFilterCallback; 39 using ::aidl::android::media::tv::tuner::ITunerFilter; 40 using ::android::hardware::EventFlag; 41 using ::android::Mutex; 42 43 using namespace std; 44 45 namespace android { 46 47 using AidlMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>; 48 using AidlMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>; 49 50 struct SharedHandleInfo { 51 native_handle_t* sharedHandle; 52 uint64_t size; 53 }; 54 55 class TunerFilterCallback : public BnTunerFilterCallback { 56 public: 57 TunerFilterCallback(sp<FilterClientCallback> filterClientCallback); 58 Status onFilterStatus(DemuxFilterStatus status); 59 Status onFilterEvent(const vector<DemuxFilterEvent>& filterEvents); 60 61 private: 62 sp<FilterClientCallback> mFilterClientCallback; 63 }; 64 65 struct FilterClient : public RefBase { 66 67 public: 68 FilterClient(DemuxFilterType type, shared_ptr<ITunerFilter> tunerFilter); 69 ~FilterClient(); 70 71 /** 72 * Read size of data from filter FMQ into buffer. 73 * 74 * @return the actual reading size. -1 if failed to read. 75 */ 76 int64_t read(int8_t* buffer, int64_t size); 77 78 /** 79 * Get the a/v shared memory handle information 80 */ 81 SharedHandleInfo getAvSharedHandleInfo(); 82 83 /** 84 * Configure the filter. 85 */ 86 Result configure(DemuxFilterSettings configure); 87 88 /** 89 * Configure the monitor event of the Filter. 90 */ 91 Result configureMonitorEvent(int32_t monitorEventType); 92 93 /** 94 * Configure the context id of the IP Filter. 95 */ 96 Result configureIpFilterContextId(int32_t cid); 97 98 /** 99 * Configure the stream type of the media Filter. 100 */ 101 Result configureAvStreamType(AvStreamType avStreamType); 102 103 /** 104 * Start the filter. 105 */ 106 Result start(); 107 108 /** 109 * Stop the filter. 110 */ 111 Result stop(); 112 113 /** 114 * Flush the filter. 115 */ 116 Result flush(); 117 118 /** 119 * Get the 32-bit filter Id. 120 */ 121 Result getId(int32_t& id); 122 123 /** 124 * Get the 64-bit filter Id. 125 */ 126 Result getId64Bit(int64_t& id); 127 128 /** 129 * Release the handle reported by the HAL for AV memory. 130 */ 131 Result releaseAvHandle(native_handle_t* handle, uint64_t avDataId); 132 133 /** 134 * Set the filter's data source. 135 */ 136 Result setDataSource(sp<FilterClient> filterClient); 137 138 /** 139 * Get the Aidl filter to build up filter linkage. 140 */ getAidlFilterFilterClient141 shared_ptr<ITunerFilter> getAidlFilter() { return mTunerFilter; } 142 143 /** 144 * Close a new interface of ITunerFilter. 145 */ 146 Result close(); 147 148 /** 149 * Accquire a new SharedFiler token. 150 */ 151 string acquireSharedFilterToken(); 152 153 /** 154 * Release SharedFiler token. 155 */ 156 Result freeSharedFilterToken(const string& filterToken); 157 158 /** 159 * Set a filter delay hint. 160 */ 161 Result setDelayHint(const FilterDelayHint& hint); 162 163 private: 164 Result getFilterMq(); 165 int64_t copyData(int8_t* buffer, int64_t size); 166 void checkIsMediaFilter(DemuxFilterType type); 167 void checkIsPassthroughFilter(DemuxFilterSettings configure); 168 void handleAvShareMemory(); 169 void closeAvSharedMemory(); 170 171 /** 172 * An AIDL Tuner Filter Singleton assigned at the first time when the Tuner Client 173 * opens a filter. Default null when Tuner Service does not exist. 174 */ 175 shared_ptr<ITunerFilter> mTunerFilter; 176 177 AidlMQ* mFilterMQ = nullptr; 178 EventFlag* mFilterMQEventFlag = nullptr; 179 180 native_handle_t* mAvSharedHandle; 181 uint64_t mAvSharedMemSize; 182 bool mIsMediaFilter; 183 bool mIsPassthroughFilter; 184 Mutex mLock; 185 }; 186 } // namespace android 187 188 #endif // _ANDROID_MEDIA_TV_FILTER_CLIENT_H_ 189