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