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