blob: a23063f8ade8a8083af9a1acf882a7a3e28ec141 [file] [log] [blame]
Hongguang4092f2f2021-07-08 18:49:12 -07001/*
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/BnDemux.h>
sadiqsada56c98292023-11-02 16:45:31 -070020#include <aidl/android/hardware/tv/tuner/BnDvrCallback.h>
Hongguang4092f2f2021-07-08 18:49:12 -070021
22#include <fmq/AidlMessageQueue.h>
23#include <math.h>
Hongguang901aa7b2021-08-26 12:20:56 -070024#include <atomic>
Hongguang4092f2f2021-07-08 18:49:12 -070025#include <set>
Hongguang901aa7b2021-08-26 12:20:56 -070026#include <thread>
27
Hongguang4092f2f2021-07-08 18:49:12 -070028#include "Dvr.h"
29#include "Filter.h"
30#include "Frontend.h"
31#include "TimeFilter.h"
sadiqsada56c98292023-11-02 16:45:31 -070032#include "Timer.h"
Hongguang4092f2f2021-07-08 18:49:12 -070033#include "Tuner.h"
sadiqsada56c98292023-11-02 16:45:31 -070034#include "dtv_plugin.h"
Hongguang4092f2f2021-07-08 18:49:12 -070035
36using namespace std;
37
38namespace aidl {
39namespace android {
40namespace hardware {
41namespace tv {
42namespace tuner {
43
44using ::aidl::android::hardware::common::fmq::MQDescriptor;
45using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
46using ::android::AidlMessageQueue;
47using ::android::hardware::EventFlag;
48
49using FilterMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
sadiqsada56c98292023-11-02 16:45:31 -070050using AidlMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
51using AidlMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>;
Hongguang4092f2f2021-07-08 18:49:12 -070052
53class Dvr;
54class Filter;
55class Frontend;
56class TimeFilter;
57class Tuner;
58
sadiqsada56c98292023-11-02 16:45:31 -070059class DvrPlaybackCallback : public BnDvrCallback {
60 public:
61 virtual ::ndk::ScopedAStatus onPlaybackStatus(PlaybackStatus status) override {
62 ALOGD("demux.h: playback status %d", status);
63 return ndk::ScopedAStatus::ok();
64 }
65
66 virtual ::ndk::ScopedAStatus onRecordStatus(RecordStatus status) override {
67 ALOGD("Record Status %hhd", status);
68 return ndk::ScopedAStatus::ok();
69 }
70};
71
Hongguang4092f2f2021-07-08 18:49:12 -070072class Demux : public BnDemux {
73 public:
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -080074 Demux(int32_t demuxId, uint32_t filterTypes);
Hongguang4092f2f2021-07-08 18:49:12 -070075 ~Demux();
76
77 ::ndk::ScopedAStatus setFrontendDataSource(int32_t in_frontendId) override;
78 ::ndk::ScopedAStatus openFilter(const DemuxFilterType& in_type, int32_t in_bufferSize,
79 const std::shared_ptr<IFilterCallback>& in_cb,
80 std::shared_ptr<IFilter>* _aidl_return) override;
81 ::ndk::ScopedAStatus openTimeFilter(std::shared_ptr<ITimeFilter>* _aidl_return) override;
82 ::ndk::ScopedAStatus getAvSyncHwId(const std::shared_ptr<IFilter>& in_filter,
83 int32_t* _aidl_return) override;
84 ::ndk::ScopedAStatus getAvSyncTime(int32_t in_avSyncHwId, int64_t* _aidl_return) override;
85 ::ndk::ScopedAStatus close() override;
86 ::ndk::ScopedAStatus openDvr(DvrType in_type, int32_t in_bufferSize,
87 const std::shared_ptr<IDvrCallback>& in_cb,
88 std::shared_ptr<IDvr>* _aidl_return) override;
89 ::ndk::ScopedAStatus connectCiCam(int32_t in_ciCamId) override;
90 ::ndk::ScopedAStatus disconnectCiCam() override;
91
Hongguang2ecfc392021-11-23 10:29:15 -080092 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
93
Hongguang4092f2f2021-07-08 18:49:12 -070094 // Functions interacts with Tuner Service
95 void stopFrontendInput();
96 ::ndk::ScopedAStatus removeFilter(int64_t filterId);
97 bool attachRecordFilter(int64_t filterId);
98 bool detachRecordFilter(int64_t filterId);
99 ::ndk::ScopedAStatus startFilterHandler(int64_t filterId);
100 void updateFilterOutput(int64_t filterId, vector<int8_t> data);
101 void updateMediaFilterOutput(int64_t filterId, vector<int8_t> data, uint64_t pts);
102 uint16_t getFilterTpid(int64_t filterId);
103 void setIsRecording(bool isRecording);
104 bool isRecording();
105 void startFrontendInputLoop();
sadiqsada56c98292023-11-02 16:45:31 -0700106 void readIptvThreadLoop(dtv_plugin* interface, dtv_streamer* streamer, void* buf, size_t size,
107 int timeout_ms, int buffer_timeout);
Hongguang4092f2f2021-07-08 18:49:12 -0700108
109 /**
110 * A dispatcher to read and dispatch input data to all the started filters.
111 * Each filter handler handles the data filtering/output writing/filterEvent updating.
112 * Note that recording filters are not included.
113 */
114 bool startBroadcastFilterDispatcher();
115 void startBroadcastTsFilter(vector<int8_t> data);
116
117 void sendFrontendInputToRecord(vector<int8_t> data);
118 void sendFrontendInputToRecord(vector<int8_t> data, uint16_t pid, uint64_t pts);
119 bool startRecordFilterDispatcher();
120
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -0800121 void getDemuxInfo(DemuxInfo* demuxInfo);
122 int32_t getDemuxId();
123 bool isInUse();
124 void setInUse(bool inUse);
125 void setTunerService(std::shared_ptr<Tuner> tuner);
126
Hongguang4092f2f2021-07-08 18:49:12 -0700127 private:
128 // Tuner service
Hongguang Chenff2c6b02021-08-07 00:12:26 +0000129 std::shared_ptr<Tuner> mTuner;
Hongguang4092f2f2021-07-08 18:49:12 -0700130
131 // Frontend source
132 std::shared_ptr<Frontend> mFrontend;
133
134 // A struct that passes the arguments to a newly created filter thread
135 struct ThreadArgs {
136 Demux* user;
137 int64_t filterId;
138 };
139
140 static void* __threadLoopFrontend(void* user);
141 void frontendInputThreadLoop();
142
143 /**
144 * To create a FilterMQ with the next available Filter ID.
145 * Creating Event Flag at the same time.
146 * Add the successfully created/saved FilterMQ into the local list.
147 *
148 * Return false is any of the above processes fails.
149 */
150 void deleteEventFlag();
151 bool readDataFromMQ();
152
153 int32_t mDemuxId = -1;
154 int32_t mCiCamId;
155 set<int64_t> mPcrFilterIds;
156 /**
157 * Record the last used filter id. Initial value is -1.
158 * Filter Id starts with 0.
159 */
160 int64_t mLastUsedFilterId = -1;
161 /**
162 * Record all the used playback filter Ids.
163 * Any removed filter id should be removed from this set.
164 */
165 set<int64_t> mPlaybackFilterIds;
166 /**
167 * Record all the attached record filter Ids.
168 * Any removed filter id should be removed from this set.
169 */
170 set<int64_t> mRecordFilterIds;
171 /**
172 * A list of created Filter sp.
173 * The array number is the filter ID.
174 */
175 std::map<int64_t, std::shared_ptr<Filter>> mFilters;
176
177 /**
178 * Local reference to the opened Timer Filter instance.
179 */
180 std::shared_ptr<TimeFilter> mTimeFilter;
181
182 /**
183 * Local reference to the opened DVR object.
184 */
185 std::shared_ptr<Dvr> mDvrPlayback;
186 std::shared_ptr<Dvr> mDvrRecord;
187
188 // Thread handlers
Hongguang901aa7b2021-08-26 12:20:56 -0700189 std::thread mFrontendInputThread;
sadiqsada56c98292023-11-02 16:45:31 -0700190 std::thread mDemuxIptvReadThread;
191
192 // track whether the DVR FMQ for IPTV Playback is full
193 bool mIsIptvDvrFMQFull = false;
Hongguang901aa7b2021-08-26 12:20:56 -0700194
Hongguang4092f2f2021-07-08 18:49:12 -0700195 /**
196 * If a specific filter's writing loop is still running
197 */
Hongguang901aa7b2021-08-26 12:20:56 -0700198 std::atomic<bool> mFrontendInputThreadRunning;
sadiqsada56c98292023-11-02 16:45:31 -0700199 std::atomic<bool> mDemuxIptvReadThreadRunning;
Hongguang901aa7b2021-08-26 12:20:56 -0700200 std::atomic<bool> mKeepFetchingDataFromFrontend;
201
Hongguang4092f2f2021-07-08 18:49:12 -0700202 /**
203 * If the dvr recording is running.
204 */
205 bool mIsRecording = false;
206 /**
207 * Lock to protect writes to the FMQs
208 */
209 std::mutex mWriteLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700210
211 // temp handle single PES filter
212 // TODO handle mulptiple Pes filters
213 int mPesSizeLeft = 0;
214 vector<uint8_t> mPesOutput;
215
216 const bool DEBUG_DEMUX = false;
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -0800217
218 int32_t mFilterTypes;
219 bool mInUse = false;
Hongguang4092f2f2021-07-08 18:49:12 -0700220};
221
222} // namespace tuner
223} // namespace tv
224} // namespace hardware
225} // namespace android
226} // namespace aidl