Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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_0_DEMUX_H_ |
| 18 | #define ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_ |
| 19 | |
| 20 | #include <android/hardware/tv/tuner/1.0/IDemux.h> |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 21 | #include <fmq/MessageQueue.h> |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 22 | #include <set> |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace tv { |
| 29 | namespace tuner { |
| 30 | namespace V1_0 { |
| 31 | namespace implementation { |
| 32 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 33 | using ::android::hardware::EventFlag; |
| 34 | using ::android::hardware::kSynchronizedReadWrite; |
| 35 | using ::android::hardware::MessageQueue; |
| 36 | using ::android::hardware::MQDescriptorSync; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 37 | using ::android::hardware::tv::tuner::V1_0::IDemux; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 38 | using ::android::hardware::tv::tuner::V1_0::IDemuxCallback; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 39 | using ::android::hardware::tv::tuner::V1_0::Result; |
| 40 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 41 | using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; |
| 42 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 43 | class Demux : public IDemux { |
| 44 | public: |
| 45 | Demux(uint32_t demuxId); |
| 46 | |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 47 | ~Demux(); |
| 48 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 49 | virtual Return<Result> setFrontendDataSource(uint32_t frontendId) override; |
| 50 | |
| 51 | virtual Return<Result> close() override; |
| 52 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 53 | virtual Return<void> addFilter(DemuxFilterType type, uint32_t bufferSize, |
| 54 | const sp<IDemuxCallback>& cb, addFilter_cb _hidl_cb) override; |
| 55 | |
| 56 | virtual Return<void> getFilterQueueDesc(uint32_t filterId, |
| 57 | getFilterQueueDesc_cb _hidl_cb) override; |
| 58 | |
| 59 | virtual Return<Result> configureFilter(uint32_t filterId, |
| 60 | const DemuxFilterSettings& settings) override; |
| 61 | |
| 62 | virtual Return<Result> startFilter(uint32_t filterId) override; |
| 63 | |
| 64 | virtual Return<Result> stopFilter(uint32_t filterId) override; |
| 65 | |
| 66 | virtual Return<Result> flushFilter(uint32_t filterId) override; |
| 67 | |
| 68 | virtual Return<Result> removeFilter(uint32_t filterId) override; |
| 69 | |
| 70 | virtual Return<void> getAvSyncHwId(uint32_t filterId, getAvSyncHwId_cb _hidl_cb) override; |
| 71 | |
| 72 | virtual Return<void> getAvSyncTime(AvSyncHwId avSyncHwId, getAvSyncTime_cb _hidl_cb) override; |
| 73 | |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 74 | virtual Return<Result> addInput(uint32_t bufferSize, const sp<IDemuxCallback>& cb) override; |
| 75 | |
| 76 | virtual Return<void> getInputQueueDesc(getInputQueueDesc_cb _hidl_cb) override; |
| 77 | |
| 78 | virtual Return<Result> configureInput(const DemuxInputSettings& settings) override; |
| 79 | |
| 80 | virtual Return<Result> startInput() override; |
| 81 | |
| 82 | virtual Return<Result> stopInput() override; |
| 83 | |
| 84 | virtual Return<Result> flushInput() override; |
| 85 | |
| 86 | virtual Return<Result> removeInput() override; |
| 87 | |
| 88 | virtual Return<Result> addOutput(uint32_t bufferSize, const sp<IDemuxCallback>& cb) override; |
| 89 | |
| 90 | virtual Return<void> getOutputQueueDesc(getOutputQueueDesc_cb _hidl_cb) override; |
| 91 | |
| 92 | virtual Return<Result> configureOutput(const DemuxOutputSettings& settings) override; |
| 93 | |
| 94 | virtual Return<Result> attachOutputTsFilter(uint32_t filterId) override; |
| 95 | |
| 96 | virtual Return<Result> detachOutputTsFilter(uint32_t filterId) override; |
| 97 | |
| 98 | virtual Return<Result> startOutput() override; |
| 99 | |
| 100 | virtual Return<Result> stopOutput() override; |
| 101 | |
| 102 | virtual Return<Result> flushOutput() override; |
| 103 | |
| 104 | virtual Return<Result> removeOutput() override; |
| 105 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 106 | private: |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 107 | // A struct that passes the arguments to a newly created filter thread |
| 108 | struct ThreadArgs { |
| 109 | Demux* user; |
| 110 | uint32_t filterId; |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Filter handlers to handle the data filtering. |
| 115 | * They are also responsible to write the filtered output into the filter FMQ |
| 116 | * and update the filterEvent bound with the same filterId. |
| 117 | */ |
| 118 | Result startSectionFilterHandler(uint32_t filterId, vector<uint8_t> data); |
| 119 | Result startPesFilterHandler(uint32_t filterId); |
| 120 | Result startTsFilterHandler(); |
| 121 | Result startMediaFilterHandler(uint32_t filterId); |
| 122 | Result startRecordFilterHandler(uint32_t filterId); |
| 123 | Result startPcrFilterHandler(); |
| 124 | Result startFilterLoop(uint32_t filterId); |
| 125 | |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 126 | /** |
| 127 | * To create a FilterMQ with the the next available Filter ID. |
| 128 | * Creating Event Flag at the same time. |
| 129 | * Add the successfully created/saved FilterMQ into the local list. |
| 130 | * |
| 131 | * Return false is any of the above processes fails. |
| 132 | */ |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 133 | bool createFilterMQ(uint32_t bufferSize, uint32_t filterId); |
| 134 | bool createMQ(FilterMQ* queue, EventFlag* eventFlag, uint32_t bufferSize); |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 135 | void deleteEventFlag(); |
| 136 | bool writeDataToFilterMQ(const std::vector<uint8_t>& data, uint32_t filterId); |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 137 | bool readDataFromMQ(); |
| 138 | bool writeSectionsAndCreateEvent(uint32_t filterId, vector<uint8_t> data); |
| 139 | /** |
| 140 | * A dispatcher to read and dispatch input data to all the started filters. |
| 141 | * Each filter handler handles the data filtering/output writing/filterEvent updating. |
| 142 | */ |
| 143 | bool filterAndOutputData(); |
| 144 | static void* __threadLoopFilter(void* data); |
| 145 | static void* __threadLoopInput(void* user); |
| 146 | void filterThreadLoop(uint32_t filterId); |
| 147 | void inputThreadLoop(); |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 148 | |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 149 | uint32_t mDemuxId; |
| 150 | uint32_t mSourceFrontendId; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 151 | /** |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 152 | * Record the last used filter id. Initial value is -1. |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 153 | * Filter Id starts with 0. |
| 154 | */ |
| 155 | uint32_t mLastUsedFilterId = -1; |
| 156 | /** |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 157 | * Record all the used filter Ids. |
| 158 | * Any removed filter id should be removed from this set. |
| 159 | */ |
| 160 | set<uint32_t> mUsedFilterIds; |
| 161 | /** |
| 162 | * Record all the unused filter Ids within mLastUsedFilterId. |
| 163 | * Removed filter Id should be added into this set. |
| 164 | * When this set is not empty, ids here should be allocated first |
| 165 | * and added into usedFilterIds. |
| 166 | */ |
| 167 | set<uint32_t> mUnusedFilterIds; |
| 168 | /** |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 169 | * A list of created FilterMQ ptrs. |
| 170 | * The array number is the filter ID. |
| 171 | */ |
| 172 | vector<unique_ptr<FilterMQ>> mFilterMQs; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 173 | vector<EventFlag*> mFilterEventFlags; |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 174 | vector<DemuxFilterEvent> mFilterEvents; |
| 175 | unique_ptr<FilterMQ> mInputMQ; |
| 176 | unique_ptr<FilterMQ> mOutputMQ; |
| 177 | EventFlag* mInputEventFlag; |
| 178 | EventFlag* mOutputEventFlag; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Demux callbacks used on filter events or IO buffer status |
| 181 | */ |
| 182 | vector<sp<IDemuxCallback>> mDemuxCallbacks; |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 183 | sp<IDemuxCallback> mInputCallback; |
| 184 | sp<IDemuxCallback> mOutputCallback; |
| 185 | // Thread handlers |
| 186 | pthread_t mInputThread; |
| 187 | pthread_t mOutputThread; |
| 188 | vector<pthread_t> mFilterThreads; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 189 | /** |
| 190 | * If a specific filter's writing loop is still running |
| 191 | */ |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 192 | vector<bool> mFilterThreadRunning; |
| 193 | bool mInputThreadRunning; |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Lock to protect writes to the FMQs |
| 196 | */ |
| 197 | std::mutex mWriteLock; |
| 198 | /** |
Amy | a488529 | 2019-09-06 10:30:53 -0700 | [diff] [blame] | 199 | * Lock to protect writes to the filter event |
| 200 | */ |
| 201 | std::mutex mFilterEventLock; |
| 202 | /** |
Amy | a609d5a | 2019-08-23 14:38:31 -0700 | [diff] [blame] | 203 | * How many times a filter should write |
| 204 | * TODO make this dynamic/random/can take as a parameter |
| 205 | */ |
| 206 | const uint16_t SECTION_WRITE_COUNT = 10; |
Amy | fd4243a | 2019-08-16 16:01:27 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | } // namespace implementation |
| 210 | } // namespace V1_0 |
| 211 | } // namespace tuner |
| 212 | } // namespace tv |
| 213 | } // namespace hardware |
| 214 | } // namespace android |
| 215 | |
| 216 | #endif // ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_ |