Amy Zhang | bb94eeb | 2020-07-09 22:48:04 -0700 | [diff] [blame] | 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_DVR_H_ |
| 18 | #define ANDROID_HARDWARE_TV_TUNER_V1_1_DVR_H_ |
| 19 | |
| 20 | #include <fmq/MessageQueue.h> |
| 21 | #include <math.h> |
| 22 | #include <set> |
| 23 | #include "Demux.h" |
| 24 | #include "Frontend.h" |
| 25 | #include "Tuner.h" |
| 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace tv { |
| 32 | namespace tuner { |
| 33 | namespace V1_0 { |
| 34 | namespace implementation { |
| 35 | |
| 36 | using ::android::hardware::EventFlag; |
| 37 | using ::android::hardware::kSynchronizedReadWrite; |
| 38 | using ::android::hardware::MessageQueue; |
| 39 | using ::android::hardware::MQDescriptorSync; |
| 40 | |
| 41 | using DvrMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>; |
| 42 | |
| 43 | struct MediaEsMetaData { |
| 44 | bool isAudio; |
| 45 | int startIndex; |
| 46 | int len; |
| 47 | int pts; |
| 48 | }; |
| 49 | |
| 50 | class Demux; |
| 51 | class Filter; |
| 52 | class Frontend; |
| 53 | class Tuner; |
| 54 | |
| 55 | class Dvr : public IDvr { |
| 56 | public: |
| 57 | Dvr(); |
| 58 | |
| 59 | Dvr(DvrType type, uint32_t bufferSize, const sp<IDvrCallback>& cb, sp<Demux> demux); |
| 60 | |
| 61 | ~Dvr(); |
| 62 | |
| 63 | virtual Return<void> getQueueDesc(getQueueDesc_cb _hidl_cb) override; |
| 64 | |
| 65 | virtual Return<Result> configure(const DvrSettings& settings) override; |
| 66 | |
| 67 | virtual Return<Result> attachFilter(const sp<IFilter>& filter) override; |
| 68 | |
| 69 | virtual Return<Result> detachFilter(const sp<IFilter>& filter) 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> close() override; |
| 78 | |
| 79 | /** |
| 80 | * To create a DvrMQ and its Event Flag. |
| 81 | * |
| 82 | * Return false is any of the above processes fails. |
| 83 | */ |
| 84 | bool createDvrMQ(); |
| 85 | void sendBroadcastInputToDvrRecord(vector<uint8_t> byteBuffer); |
| 86 | bool writeRecordFMQ(const std::vector<uint8_t>& data); |
| 87 | bool addPlaybackFilter(uint64_t filterId, sp<IFilter> filter); |
| 88 | bool removePlaybackFilter(uint64_t filterId); |
| 89 | bool readPlaybackFMQ(bool isVirtualFrontend, bool isRecording); |
| 90 | bool processEsDataOnPlayback(bool isVirtualFrontend, bool isRecording); |
| 91 | bool startFilterDispatcher(bool isVirtualFrontend, bool isRecording); |
| 92 | EventFlag* getDvrEventFlag(); |
| 93 | DvrSettings getSettings() { return mDvrSettings; } |
| 94 | |
| 95 | private: |
| 96 | // Demux service |
| 97 | sp<Demux> mDemux; |
| 98 | |
| 99 | DvrType mType; |
| 100 | uint32_t mBufferSize; |
| 101 | sp<IDvrCallback> mCallback; |
| 102 | std::map<uint64_t, sp<IFilter>> mFilters; |
| 103 | |
| 104 | void deleteEventFlag(); |
| 105 | bool readDataFromMQ(); |
| 106 | void getMetaDataValue(int& index, uint8_t* dataOutputBuffer, int& value); |
| 107 | void maySendPlaybackStatusCallback(); |
| 108 | void maySendRecordStatusCallback(); |
| 109 | PlaybackStatus checkPlaybackStatusChange(uint32_t availableToWrite, uint32_t availableToRead, |
| 110 | uint32_t highThreshold, uint32_t lowThreshold); |
| 111 | RecordStatus checkRecordStatusChange(uint32_t availableToWrite, uint32_t availableToRead, |
| 112 | uint32_t highThreshold, uint32_t lowThreshold); |
| 113 | /** |
| 114 | * A dispatcher to read and dispatch input data to all the started filters. |
| 115 | * Each filter handler handles the data filtering/output writing/filterEvent updating. |
| 116 | */ |
| 117 | void startTpidFilter(vector<uint8_t> data); |
| 118 | static void* __threadLoopPlayback(void* user); |
| 119 | static void* __threadLoopRecord(void* user); |
| 120 | void playbackThreadLoop(); |
| 121 | void recordThreadLoop(); |
| 122 | |
| 123 | unique_ptr<DvrMQ> mDvrMQ; |
| 124 | EventFlag* mDvrEventFlag; |
| 125 | /** |
| 126 | * Demux callbacks used on filter events or IO buffer status |
| 127 | */ |
| 128 | bool mDvrConfigured = false; |
| 129 | DvrSettings mDvrSettings; |
| 130 | |
| 131 | // Thread handlers |
| 132 | pthread_t mDvrThread; |
| 133 | |
| 134 | // FMQ status local records |
| 135 | PlaybackStatus mPlaybackStatus; |
| 136 | RecordStatus mRecordStatus; |
| 137 | /** |
| 138 | * If a specific filter's writing loop is still running |
| 139 | */ |
| 140 | bool mDvrThreadRunning; |
| 141 | bool mKeepFetchingDataFromFrontend; |
| 142 | /** |
| 143 | * Lock to protect writes to the FMQs |
| 144 | */ |
| 145 | std::mutex mWriteLock; |
| 146 | /** |
| 147 | * Lock to protect writes to the input status |
| 148 | */ |
| 149 | std::mutex mPlaybackStatusLock; |
| 150 | std::mutex mRecordStatusLock; |
| 151 | std::mutex mDvrThreadLock; |
| 152 | |
| 153 | const bool DEBUG_DVR = false; |
| 154 | |
| 155 | // Booleans to check if recording is running. |
| 156 | // Recording is ready when both of the following are set to true. |
| 157 | bool mIsRecordStarted = false; |
| 158 | }; |
| 159 | |
| 160 | } // namespace implementation |
| 161 | } // namespace V1_0 |
| 162 | } // namespace tuner |
| 163 | } // namespace tv |
| 164 | } // namespace hardware |
| 165 | } // namespace android |
| 166 | |
| 167 | #endif // ANDROID_HARDWARE_TV_TUNER_V1_1_DVR_H_ |