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 #ifndef ANDROID_HARDWARE_TV_TUNER_V1_1_FILTER_H_ 18 #define ANDROID_HARDWARE_TV_TUNER_V1_1_FILTER_H_ 19 20 #include <android/hardware/tv/tuner/1.1/IFilter.h> 21 #include <android/hardware/tv/tuner/1.1/IFilterCallback.h> 22 #include <fmq/MessageQueue.h> 23 #include <inttypes.h> 24 #include <ion/ion.h> 25 #include <math.h> 26 #include <sys/stat.h> 27 #include <set> 28 #include "Demux.h" 29 #include "Dvr.h" 30 #include "Frontend.h" 31 32 using namespace std; 33 34 namespace android { 35 namespace hardware { 36 namespace tv { 37 namespace tuner { 38 namespace V1_0 { 39 namespace implementation { 40 41 using ::android::hardware::EventFlag; 42 using ::android::hardware::kSynchronizedReadWrite; 43 using ::android::hardware::MessageQueue; 44 using ::android::hardware::MQDescriptorSync; 45 46 using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; 47 const uint32_t BUFFER_SIZE_16M = 0x1000000; 48 49 class Demux; 50 class Dvr; 51 52 class Filter : public V1_1::IFilter { 53 public: 54 Filter(); 55 56 Filter(DemuxFilterType type, uint64_t filterId, uint32_t bufferSize, 57 const sp<IFilterCallback>& cb, sp<Demux> demux); 58 59 ~Filter(); 60 61 virtual Return<void> getId64Bit(getId64Bit_cb _hidl_cb) override; 62 63 virtual Return<void> getId(getId_cb _hidl_cb) override; 64 65 virtual Return<Result> setDataSource(const sp<V1_0::IFilter>& filter) override; 66 67 virtual Return<void> getQueueDesc(getQueueDesc_cb _hidl_cb) override; 68 69 virtual Return<Result> configure(const DemuxFilterSettings& settings) override; 70 71 virtual Return<Result> start() override; 72 73 virtual Return<Result> stop() override; 74 75 virtual Return<Result> flush() override; 76 77 virtual Return<Result> releaseAvHandle(const hidl_handle& avMemory, uint64_t avDataId) override; 78 79 virtual Return<Result> close() override; 80 81 virtual Return<Result> configureIpCid(uint32_t ipCid) override; 82 83 virtual Return<void> getAvSharedHandle(getAvSharedHandle_cb _hidl_cb) override; 84 85 virtual Return<Result> configureAvStreamType(const V1_1::AvStreamType& avStreamType) override; 86 87 virtual Return<Result> configureMonitorEvent(uint32_t monitorEventTypes) override; 88 89 /** 90 * To create a FilterMQ and its Event Flag. 91 * 92 * Return false is any of the above processes fails. 93 */ 94 bool createFilterMQ(); 95 uint16_t getTpid(); 96 void updateFilterOutput(vector<uint8_t> data); 97 void updateRecordOutput(vector<uint8_t> data); 98 void updatePts(uint64_t pts); 99 Result startFilterHandler(); 100 Result startRecordFilterHandler(); 101 void attachFilterToRecord(const sp<Dvr> dvr); 102 void detachFilterFromRecord(); 103 void freeAvHandle(); 104 void freeSharedAvHandle(); isMediaFilter()105 bool isMediaFilter() { return mIsMediaFilter; }; isPcrFilter()106 bool isPcrFilter() { return mIsPcrFilter; }; isRecordFilter()107 bool isRecordFilter() { return mIsRecordFilter; }; 108 109 private: 110 // Tuner service 111 sp<Demux> mDemux; 112 // Dvr reference once the filter is attached to any 113 sp<Dvr> mDvr = nullptr; 114 /** 115 * Filter callbacks used on filter events or FMQ status 116 */ 117 sp<IFilterCallback> mCallback = nullptr; 118 119 /** 120 * V1_1 Filter callbacks used on filter events or FMQ status 121 */ 122 sp<V1_1::IFilterCallback> mCallback_1_1 = nullptr; 123 124 uint64_t mFilterId; 125 uint32_t mCid = static_cast<uint32_t>(V1_1::Constant::INVALID_IP_FILTER_CONTEXT_ID); 126 uint32_t mBufferSize; 127 DemuxFilterType mType; 128 bool mIsMediaFilter = false; 129 bool mIsPcrFilter = false; 130 bool mIsRecordFilter = false; 131 DemuxFilterSettings mFilterSettings; 132 133 uint16_t mTpid; 134 sp<V1_0::IFilter> mDataSource; 135 bool mIsDataSourceDemux = true; 136 vector<uint8_t> mFilterOutput; 137 vector<uint8_t> mRecordFilterOutput; 138 uint64_t mPts = 0; 139 unique_ptr<FilterMQ> mFilterMQ; 140 bool mIsUsingFMQ = false; 141 EventFlag* mFilterEventFlag; 142 DemuxFilterEvent mFilterEvent; 143 V1_1::DemuxFilterEventExt mFilterEventExt; 144 145 // Thread handlers 146 pthread_t mFilterThread; 147 148 // FMQ status local records 149 DemuxFilterStatus mFilterStatus; 150 /** 151 * If a specific filter's writing loop is still running 152 */ 153 bool mFilterThreadRunning; 154 bool mKeepFetchingDataFromFrontend; 155 156 /** 157 * How many times a filter should write 158 * TODO make this dynamic/random/can take as a parameter 159 */ 160 const uint16_t SECTION_WRITE_COUNT = 10; 161 162 bool DEBUG_FILTER = false; 163 164 /** 165 * Filter handlers to handle the data filtering. 166 * They are also responsible to write the filtered output into the filter FMQ 167 * and update the filterEvent bound with the same filterId. 168 */ 169 Result startSectionFilterHandler(); 170 Result startPesFilterHandler(); 171 Result startTsFilterHandler(); 172 Result startMediaFilterHandler(); 173 Result startPcrFilterHandler(); 174 Result startTemiFilterHandler(); 175 Result startFilterLoop(); 176 177 void deleteEventFlag(); 178 bool writeDataToFilterMQ(const std::vector<uint8_t>& data); 179 bool readDataFromMQ(); 180 bool writeSectionsAndCreateEvent(vector<uint8_t> data); 181 void maySendFilterStatusCallback(); 182 DemuxFilterStatus checkFilterStatusChange(uint32_t availableToWrite, uint32_t availableToRead, 183 uint32_t highThreshold, uint32_t lowThreshold); 184 /** 185 * A dispatcher to read and dispatch input data to all the started filters. 186 * Each filter handler handles the data filtering/output writing/filterEvent updating. 187 */ 188 void startTsFilter(vector<uint8_t> data); 189 bool startFilterDispatcher(); 190 static void* __threadLoopFilter(void* user); 191 void filterThreadLoop(); 192 193 int createAvIonFd(int size); 194 uint8_t* getIonBuffer(int fd, int size); 195 native_handle_t* createNativeHandle(int fd); 196 Result createMediaFilterEventWithIon(vector<uint8_t> output); 197 Result createIndependentMediaEvents(vector<uint8_t> output); 198 Result createShareMemMediaEvents(vector<uint8_t> output); 199 bool sameFile(int fd1, int fd2); 200 201 DemuxFilterEvent createMediaEvent(); 202 DemuxFilterEvent createTsRecordEvent(); 203 V1_1::DemuxFilterEventExt createTsRecordEventExt(); 204 DemuxFilterEvent createMmtpRecordEvent(); 205 V1_1::DemuxFilterEventExt createMmtpRecordEventExt(); 206 DemuxFilterEvent createSectionEvent(); 207 DemuxFilterEvent createPesEvent(); 208 DemuxFilterEvent createDownloadEvent(); 209 DemuxFilterEvent createIpPayloadEvent(); 210 DemuxFilterEvent createTemiEvent(); 211 V1_1::DemuxFilterEventExt createMonitorEvent(); 212 V1_1::DemuxFilterEventExt createRestartEvent(); 213 /** 214 * Lock to protect writes to the FMQs 215 */ 216 std::mutex mWriteLock; 217 /** 218 * Lock to protect writes to the filter event 219 */ 220 // TODO make each filter separate event lock 221 std::mutex mFilterEventLock; 222 /** 223 * Lock to protect writes to the input status 224 */ 225 std::mutex mFilterStatusLock; 226 std::mutex mFilterThreadLock; 227 std::mutex mFilterOutputLock; 228 std::mutex mRecordFilterOutputLock; 229 230 // temp handle single PES filter 231 // TODO handle mulptiple Pes filters 232 int mPesSizeLeft = 0; 233 vector<uint8_t> mPesOutput; 234 235 // A map from data id to ion handle 236 std::map<uint64_t, int> mDataId2Avfd; 237 uint64_t mLastUsedDataId = 1; 238 int mAvBufferCopyCount = 0; 239 240 // Shared A/V memory handle 241 hidl_handle mSharedAvMemHandle; 242 bool mUsingSharedAvMem = false; 243 uint32_t mSharedAvMemOffset = 0; 244 245 uint32_t mAudioStreamType; 246 uint32_t mVideoStreamType; 247 248 // Scrambling status to be monitored 249 uint32_t mStatuses = 0; 250 251 bool mConfigured = false; 252 int mStartId = 0; 253 uint8_t mScramblingStatusMonitored = 0; 254 uint8_t mIpCidMonitored = 0; 255 }; 256 257 } // namespace implementation 258 } // namespace V1_0 259 } // namespace tuner 260 } // namespace tv 261 } // namespace hardware 262 } // namespace android 263 264 #endif // ANDROID_HARDWARE_TV_TUNER_V1_1_FILTER_H_ 265