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 #include <android-base/logging.h> 18 #include <android/hardware/tv/tuner/1.0/IFilter.h> 19 #include <android/hardware/tv/tuner/1.0/IFilterCallback.h> 20 #include <android/hardware/tv/tuner/1.0/ITuner.h> 21 #include <android/hardware/tv/tuner/1.0/types.h> 22 #include <fmq/MessageQueue.h> 23 #include <gtest/gtest.h> 24 #include <hidl/HidlSupport.h> 25 #include <hidl/HidlTransportSupport.h> 26 #include <hidl/Status.h> 27 #include <utils/Condition.h> 28 #include <utils/Mutex.h> 29 #include <map> 30 31 using android::Condition; 32 using android::Mutex; 33 using android::sp; 34 using android::hardware::EventFlag; 35 using android::hardware::hidl_handle; 36 using android::hardware::hidl_string; 37 using android::hardware::hidl_vec; 38 using android::hardware::kSynchronizedReadWrite; 39 using android::hardware::MessageQueue; 40 using android::hardware::MQDescriptorSync; 41 using android::hardware::Return; 42 using android::hardware::Void; 43 using android::hardware::tv::tuner::V1_0::DemuxFilterEvent; 44 using android::hardware::tv::tuner::V1_0::DemuxFilterMainType; 45 using android::hardware::tv::tuner::V1_0::DemuxFilterMediaEvent; 46 using android::hardware::tv::tuner::V1_0::DemuxFilterPesDataSettings; 47 using android::hardware::tv::tuner::V1_0::DemuxFilterPesEvent; 48 using android::hardware::tv::tuner::V1_0::DemuxFilterRecordSettings; 49 using android::hardware::tv::tuner::V1_0::DemuxFilterSectionEvent; 50 using android::hardware::tv::tuner::V1_0::DemuxFilterSectionSettings; 51 using android::hardware::tv::tuner::V1_0::DemuxFilterSettings; 52 using android::hardware::tv::tuner::V1_0::DemuxFilterStatus; 53 using android::hardware::tv::tuner::V1_0::DemuxFilterType; 54 using android::hardware::tv::tuner::V1_0::DemuxQueueNotifyBits; 55 using android::hardware::tv::tuner::V1_0::DemuxTsFilterSettings; 56 using android::hardware::tv::tuner::V1_0::DemuxTsFilterType; 57 using android::hardware::tv::tuner::V1_0::IDemux; 58 using android::hardware::tv::tuner::V1_0::IFilter; 59 using android::hardware::tv::tuner::V1_0::IFilterCallback; 60 using android::hardware::tv::tuner::V1_0::ITimeFilter; 61 using android::hardware::tv::tuner::V1_0::ITuner; 62 using android::hardware::tv::tuner::V1_0::Result; 63 64 using ::testing::AssertionResult; 65 66 using namespace std; 67 68 enum FilterEventType : uint8_t { 69 UNDEFINED, 70 SECTION, 71 MEDIA, 72 PES, 73 RECORD, 74 MMTPRECORD, 75 DOWNLOAD, 76 TEMI, 77 }; 78 79 using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; 80 using MQDesc = MQDescriptorSync<uint8_t>; 81 82 #define WAIT_TIMEOUT 3000000000 83 84 class FilterCallback : public IFilterCallback { 85 public: onFilterEvent(const DemuxFilterEvent & filterEvent)86 virtual Return<void> onFilterEvent(const DemuxFilterEvent& filterEvent) override { 87 android::Mutex::Autolock autoLock(mMsgLock); 88 // Temprarily we treat the first coming back filter data on the matching pid a success 89 // once all of the MQ are cleared, means we got all the expected output 90 mFilterEvent = filterEvent; 91 readFilterEventData(); 92 mPidFilterOutputCount++; 93 // mFilterIdToMQ.erase(filterEvent.filterId); 94 95 // startFilterEventThread(filterEvent); 96 mMsgCondition.signal(); 97 return Void(); 98 } 99 onFilterStatus(const DemuxFilterStatus)100 virtual Return<void> onFilterStatus(const DemuxFilterStatus /*status*/) override { 101 return Void(); 102 } 103 setFilterId(uint32_t filterId)104 void setFilterId(uint32_t filterId) { mFilterId = filterId; } setFilterInterface(sp<IFilter> filter)105 void setFilterInterface(sp<IFilter> filter) { mFilter = filter; } setFilterEventType(FilterEventType type)106 void setFilterEventType(FilterEventType type) { mFilterEventType = type; } 107 108 void testFilterDataOutput(); 109 110 void startFilterEventThread(DemuxFilterEvent event); 111 static void* __threadLoopFilter(void* threadArgs); 112 void filterThreadLoop(DemuxFilterEvent& event); 113 114 void updateFilterMQ(MQDesc& filterMQDescriptor); 115 void updateGoldenOutputMap(string goldenOutputFile); 116 bool readFilterEventData(); 117 bool dumpAvData(DemuxFilterMediaEvent event); 118 119 private: 120 struct FilterThreadArgs { 121 FilterCallback* user; 122 DemuxFilterEvent event; 123 }; 124 uint16_t mDataLength = 0; 125 std::vector<uint8_t> mDataOutputBuffer; 126 127 string mFilterIdToGoldenOutput; 128 129 uint32_t mFilterId; 130 sp<IFilter> mFilter; 131 FilterEventType mFilterEventType; 132 std::unique_ptr<FilterMQ> mFilterMQ; 133 EventFlag* mFilterMQEventFlag; 134 DemuxFilterEvent mFilterEvent; 135 136 android::Mutex mMsgLock; 137 android::Mutex mFilterOutputLock; 138 android::Condition mMsgCondition; 139 android::Condition mFilterOutputCondition; 140 141 pthread_t mFilterThread; 142 143 int mPidFilterOutputCount = 0; 144 }; 145 146 class FilterTests { 147 public: setService(sp<ITuner> tuner)148 void setService(sp<ITuner> tuner) { mService = tuner; } setDemux(sp<IDemux> demux)149 void setDemux(sp<IDemux> demux) { mDemux = demux; } getFilterById(uint32_t filterId)150 sp<IFilter> getFilterById(uint32_t filterId) { return mFilters[filterId]; } 151 getFilterCallbacks()152 std::map<uint32_t, sp<FilterCallback>> getFilterCallbacks() { return mFilterCallbacks; } 153 154 AssertionResult openFilterInDemux(DemuxFilterType type, uint32_t bufferSize); 155 AssertionResult openTimeFilterInDemux(); 156 AssertionResult setTimeStamp(uint64_t timeStamp); 157 AssertionResult getTimeStamp(); 158 AssertionResult getNewlyOpenedFilterId(uint32_t& filterId); 159 AssertionResult configFilter(DemuxFilterSettings setting, uint32_t filterId); 160 AssertionResult getFilterMQDescriptor(uint32_t filterId); 161 AssertionResult setFilterDataSource(uint32_t sourceFilterId, uint32_t sinkFilterId); 162 AssertionResult setFilterDataSourceToDemux(uint32_t filterId); 163 AssertionResult startFilter(uint32_t filterId); 164 AssertionResult clearTimeStamp(); 165 AssertionResult stopFilter(uint32_t filterId); 166 AssertionResult closeFilter(uint32_t filterId); 167 AssertionResult closeTimeFilter(); 168 getFilterEventType(DemuxFilterType type)169 FilterEventType getFilterEventType(DemuxFilterType type) { 170 FilterEventType eventType = FilterEventType::UNDEFINED; 171 switch (type.mainType) { 172 case DemuxFilterMainType::TS: 173 switch (type.subType.tsFilterType()) { 174 case DemuxTsFilterType::UNDEFINED: 175 break; 176 case DemuxTsFilterType::SECTION: 177 eventType = FilterEventType::SECTION; 178 break; 179 case DemuxTsFilterType::PES: 180 eventType = FilterEventType::PES; 181 break; 182 case DemuxTsFilterType::TS: 183 break; 184 case DemuxTsFilterType::AUDIO: 185 case DemuxTsFilterType::VIDEO: 186 eventType = FilterEventType::MEDIA; 187 break; 188 case DemuxTsFilterType::PCR: 189 break; 190 case DemuxTsFilterType::RECORD: 191 eventType = FilterEventType::RECORD; 192 break; 193 case DemuxTsFilterType::TEMI: 194 eventType = FilterEventType::TEMI; 195 break; 196 } 197 break; 198 case DemuxFilterMainType::MMTP: 199 /*mmtpSettings*/ 200 break; 201 case DemuxFilterMainType::IP: 202 /*ipSettings*/ 203 break; 204 case DemuxFilterMainType::TLV: 205 /*tlvSettings*/ 206 break; 207 case DemuxFilterMainType::ALP: 208 /*alpSettings*/ 209 break; 210 default: 211 break; 212 } 213 return eventType; 214 } 215 216 protected: failure()217 static AssertionResult failure() { return ::testing::AssertionFailure(); } 218 success()219 static AssertionResult success() { return ::testing::AssertionSuccess(); } 220 221 sp<ITuner> mService; 222 sp<IFilter> mFilter; 223 sp<ITimeFilter> mTimeFilter; 224 sp<IDemux> mDemux; 225 std::map<uint32_t, sp<IFilter>> mFilters; 226 std::map<uint32_t, sp<FilterCallback>> mFilterCallbacks; 227 228 sp<FilterCallback> mFilterCallback; 229 MQDesc mFilterMQDescriptor; 230 vector<uint32_t> mUsedFilterIds; 231 232 uint32_t mFilterId = -1; 233 uint64_t mBeginTimeStamp; 234 }; 235