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