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 class Demux;
48 class Filter;
49 class Frontend;
50 class Tuner;
51 
52 class Dvr : public IDvr {
53   public:
54     Dvr();
55 
56     Dvr(DvrType type, uint32_t bufferSize, const sp<IDvrCallback>& cb, sp<Demux> demux);
57 
58     ~Dvr();
59 
60     virtual Return<void> getQueueDesc(getQueueDesc_cb _hidl_cb) override;
61 
62     virtual Return<Result> configure(const DvrSettings& settings) override;
63 
64     virtual Return<Result> attachFilter(const sp<IFilter>& filter) override;
65 
66     virtual Return<Result> detachFilter(const sp<IFilter>& filter) override;
67 
68     virtual Return<Result> start() override;
69 
70     virtual Return<Result> stop() override;
71 
72     virtual Return<Result> flush() override;
73 
74     virtual Return<Result> close() override;
75 
76     /**
77      * To create a DvrMQ and its Event Flag.
78      *
79      * Return false is any of the above processes fails.
80      */
81     bool createDvrMQ();
82     void sendBroadcastInputToDvrRecord(vector<uint8_t> byteBuffer);
83     bool writeRecordFMQ(const std::vector<uint8_t>& data);
84     bool addPlaybackFilter(uint32_t filterId, sp<IFilter> filter);
85     bool removePlaybackFilter(uint32_t filterId);
86     bool readPlaybackFMQ(bool isVirtualFrontend, bool isRecording);
87     bool startFilterDispatcher(bool isVirtualFrontend, bool isRecording);
88     EventFlag* getDvrEventFlag();
89 
90   private:
91     // Demux service
92     sp<Demux> mDemux;
93 
94     DvrType mType;
95     uint32_t mBufferSize;
96     sp<IDvrCallback> mCallback;
97     std::map<uint32_t, sp<IFilter>> mFilters;
98 
99     void deleteEventFlag();
100     bool readDataFromMQ();
101     void maySendPlaybackStatusCallback();
102     void maySendRecordStatusCallback();
103     PlaybackStatus checkPlaybackStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
104                                              uint32_t highThreshold, uint32_t lowThreshold);
105     RecordStatus checkRecordStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
106                                          uint32_t highThreshold, uint32_t lowThreshold);
107     /**
108      * A dispatcher to read and dispatch input data to all the started filters.
109      * Each filter handler handles the data filtering/output writing/filterEvent updating.
110      */
111     void startTpidFilter(vector<uint8_t> data);
112     static void* __threadLoopPlayback(void* user);
113     static void* __threadLoopRecord(void* user);
114     void playbackThreadLoop();
115     void recordThreadLoop();
116 
117     unique_ptr<DvrMQ> mDvrMQ;
118     EventFlag* mDvrEventFlag;
119     /**
120      * Demux callbacks used on filter events or IO buffer status
121      */
122     bool mDvrConfigured = false;
123     DvrSettings mDvrSettings;
124 
125     // Thread handlers
126     pthread_t mDvrThread;
127 
128     // FMQ status local records
129     PlaybackStatus mPlaybackStatus;
130     RecordStatus mRecordStatus;
131     /**
132      * If a specific filter's writing loop is still running
133      */
134     bool mDvrThreadRunning;
135     bool mKeepFetchingDataFromFrontend;
136     /**
137      * Lock to protect writes to the FMQs
138      */
139     std::mutex mWriteLock;
140     /**
141      * Lock to protect writes to the input status
142      */
143     std::mutex mPlaybackStatusLock;
144     std::mutex mRecordStatusLock;
145     std::mutex mDvrThreadLock;
146 
147     const bool DEBUG_DVR = false;
148 
149     // Booleans to check if recording is running.
150     // Recording is ready when both of the following are set to true.
151     bool mIsRecordStarted = 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_0_DVR_H_