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