• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <atomic>
25 #include <set>
26 #include <thread>
27 #include "Demux.h"
28 #include "Frontend.h"
29 #include "Tuner.h"
30 
31 using namespace std;
32 
33 namespace aidl {
34 namespace android {
35 namespace hardware {
36 namespace tv {
37 namespace tuner {
38 
39 using ::aidl::android::hardware::common::fmq::MQDescriptor;
40 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
41 using ::android::AidlMessageQueue;
42 using ::android::hardware::EventFlag;
43 
44 using DvrMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
45 
46 struct MediaEsMetaData {
47     bool isAudio;
48     int startIndex;
49     int len;
50     int pts;
51 };
52 
53 class Demux;
54 class Filter;
55 class Frontend;
56 class Tuner;
57 
58 class Dvr : public BnDvr {
59   public:
60     Dvr(DvrType type, uint32_t bufferSize, const std::shared_ptr<IDvrCallback>& cb,
61         std::shared_ptr<Demux> demux);
62     ~Dvr();
63 
64     ::ndk::ScopedAStatus getQueueDesc(
65             MQDescriptor<int8_t, SynchronizedReadWrite>* out_queue) override;
66     ::ndk::ScopedAStatus configure(const DvrSettings& in_settings) override;
67     ::ndk::ScopedAStatus attachFilter(const std::shared_ptr<IFilter>& in_filter) override;
68     ::ndk::ScopedAStatus detachFilter(const std::shared_ptr<IFilter>& in_filter) override;
69     ::ndk::ScopedAStatus start() override;
70     ::ndk::ScopedAStatus stop() override;
71     ::ndk::ScopedAStatus flush() override;
72     ::ndk::ScopedAStatus close() override;
73     ::ndk::ScopedAStatus setStatusCheckIntervalHint(int64_t in_milliseconds) override;
74 
75     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
76 
77     /**
78      * To create a DvrMQ and its Event Flag.
79      *
80      * Return false is any of the above processes fails.
81      */
82     bool createDvrMQ();
83     bool writeRecordFMQ(const std::vector<int8_t>& data);
84     bool addPlaybackFilter(int64_t filterId, std::shared_ptr<IFilter> filter);
85     bool removePlaybackFilter(int64_t filterId);
86     bool readPlaybackFMQ(bool isVirtualFrontend, bool isRecording);
87     bool processEsDataOnPlayback(bool isVirtualFrontend, bool isRecording);
88     bool startFilterDispatcher(bool isVirtualFrontend, bool isRecording);
89     EventFlag* getDvrEventFlag();
getSettings()90     DvrSettings getSettings() { return mDvrSettings; }
91 
92   private:
93     // Demux service
94     std::shared_ptr<Demux> mDemux;
95 
96     DvrType mType;
97     uint32_t mBufferSize;
98     std::shared_ptr<IDvrCallback> mCallback;
99     std::map<int64_t, std::shared_ptr<IFilter>> mFilters;
100 
101     void deleteEventFlag();
102     bool readDataFromMQ();
103     void getMetaDataValue(int& index, int8_t* dataOutputBuffer, int& value);
104     void maySendPlaybackStatusCallback();
105     void maySendRecordStatusCallback();
106     PlaybackStatus checkPlaybackStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
107                                              int64_t highThreshold, int64_t lowThreshold);
108     RecordStatus checkRecordStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
109                                          int64_t highThreshold, int64_t lowThreshold);
110     /**
111      * A dispatcher to read and dispatch input data to all the started filters.
112      * Each filter handler handles the data filtering/output writing/filterEvent updating.
113      */
114     void startTpidFilter(vector<int8_t> data);
115     void playbackThreadLoop();
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     std::thread 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     std::atomic<bool> mDvrThreadRunning;
135 
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 
146     const bool DEBUG_DVR = false;
147 };
148 
149 }  // namespace tuner
150 }  // namespace tv
151 }  // namespace hardware
152 }  // namespace android
153 }  // namespace aidl
154