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